MySQL query to return MIN() and MAX() of different columns for multiple rows -


i have prices table following layout:

id    codename    price    discount     timestamp 1     1234        599      50           2015-06-10 00:00:00 2     1234        1099     25           2015-06-11 00:00:00 3     3344        199      33           2015-06-12 00:00:00 4     5565        2499     0            2015-06-13 00:00:00 5     5565        1299     50           2015-06-14 00:00:00 

i need sql query give me single row each codename. each row must contain codename, lowest price (along associated discount , timestamp price), latest timestamp (again associated price , discount timestamp)

desired output:

codename  mintimeprice  mintimedis  mintime              latestprice  latestpricedis  latestpricetime 1234      599           50          2015-06-10 00:00:00  1099         25              2015-06-11 00:00:00 3344      199           33          2015-06-12 00:00:00  199          33              2015-06-12 00:00:00 5565      1299          50          2015-06-14 00:00:00  1299         50              2015-06-14 00:00:00 

edit: have gotten can have 2 seperate queries, 1 gets row min(price) , second gets row max(timestamp) each codename.

now need join them on same row (grouped codename) in example above.

sql fiddle of 2 queries

so after playing joins able 2 queries output onto single row per codename:

select * (   select p.*    prices p   join    (      select codename, min(price) minprice     prices group codename   ) p2   on p.price = p2.minprice , p.codename = p2.codename ) min left join (   select p.*    prices p   join    (      select codename, max(timestamp) maxtime     prices group codename   ) p2   on p.timestamp = p2.maxtime , p.codename = p2.codename ) latest on latest.codename = min.codename 

i'm sure query far perfect, give me results looking for.

sql fiddle

if there drastically wrong this, please let me know , can update.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -