sql - How to select the lowest and highest value joined in one row postgres -


i have data in similar format this

+--------+------------+-------+ | type   | variety    | price | +--------+------------+-------+ | apple  | gala       |  2.79 |  | apple  | fuji       |  0.24 |  | apple  | limbertwig |  2.87 |  | orange | valencia   |  3.59 |  | orange | navel      |  9.36 |  | pear   | bradford   |  6.05 |  | pear   | bartlett   |  2.14 |  | cherry | bing       |  2.55 |  | cherry | chelan     |  6.33 |  +--------+------------+-------+ 

and want row per type highest price , lowest price so, variety should taken form row highest price

+--------+------------+-------+-------+ | type   | variety    | min   | max   | +--------+------------+-------+-------+ | apple  | limbertwig |  0.24 |  2.87 | | orange | navel      |  9.36 |  3.59 | | pear   | bradford   |  6.05 |  2.14 | | cherry | chelan     |  6.33 |  2.55 | +--------+------------+-------+-------+ 

what best way achieve using postgres?

i found site: how select first/least/max row per group in sql, it's not quite need.

find fruits lowest price:

select distinct on (type) type, variety, price fruits order 1, 3;   type   | variety  | price  --------+----------+-------  apple  | fuji     |  0.24  cherry | bing     |  2.55  orange | valencia |  3.59  pear   | bartlett |  2.14 (4 rows) 

find fruits highest price:

select distinct on (type) type, variety, price fruits order 1, 3 desc;   type   |  variety   | price  --------+------------+-------  apple  | limbertwig |  2.87  cherry | chelan     |  6.33  orange | navel      |  9.36  pear   | bradford   |  6.05 (4 rows) 

combine 2 queries:

select      f1.type,      f1.variety min_variety, f1.price min_price,      f2.variety max_variety, f2.price max_price (     select distinct on (type) type, variety, price     fruits     order 1, 3) f1 join (     select distinct on (type) type, variety, price     fruits     order 1, 3 desc) f2 on f1.type = f2.type   type   | min_variety | min_price | max_variety | max_price  --------+-------------+-----------+-------------+-----------  apple  | fuji        |      0.24 | limbertwig  |      2.87  cherry | bing        |      2.55 | chelan      |      6.33  orange | valencia    |      3.59 | navel       |      9.36  pear   | bartlett    |      2.14 | bradford    |      6.05 (4 rows) 

alternative cannot use postgres great features:

select      f1.type,      f1.variety min_variety, f1.price min_price,      f2.variety max_variety, f2.price max_price (     select f.type, f.variety, f.price     (         select type, min(price) minprice         fruits group type         ) x      join fruits f on f.type = x.type , f.price = x.minprice     ) f1 join (     select f.type, f.variety, f.price     (         select type, max(price) maxprice         fruits group type         ) x      join fruits f on f.type = x.type , f.price = x.maxprice     ) f2 on f1.type = f2.type order 1; 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -