sql - Get row with the smallest time interval per value in a table -


here table:

 start time            stop time                 extension ---------------------------------------------------------- 2014-03-03 10:00:00   2014-03-03 11:00:00         100 2014-03-03 10:00:00   2014-03-03 12:00:00         100 2014-03-05 10:00:00   2014-03-05 11:00:00         200 2014-03-03 10:00:00   2014-03-03 13:00:00         100 2014-03-05 10:00:00   2014-03-05 12:00:00         200 2014-03-05 10:00:00   2014-03-05 13:00:00         200 

i want smallest time interval each extension:

 start time            stop time                  extension ------------------------------------------------------------- 2014-03-03 10:00:00   2014-03-03 11:00:00         100 2014-03-05 10:00:00   2014-03-05 11:00:00         200 

how can write sql?

to row (including original columns) smallest time interval each extension (according updated question) postgres specific distinct on should convenient:

select distinct on (extension)        start_time, stop_time, extension   tbl order  extension, (stop_time - start_time); 

details in related answer:
select first row in each group group?


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -