sql - Select all rows based on alternative publisher -
i want list rows alternative publisher price ascending, see example table below.
id publisher price 1 abc 100.00 2 abc 150.00 3 abc 105.00 4 xyz 135.00 5 xyz 110.00 6 pqr 105.00 7 pqr 125.00
the expected result be:
id publisher price 1 abc 100.00 6 pqr 105.00 5 xyz 110.00 3 abc 105.00 7 pqr 125.00 4 xyz 135.00 2 abc 150.00
what required sql?
this should it:
select id, publisher, price ( select id, publisher, price, row_number() on (partition publisher order price) rn publisher ) t order rn, publisher, price
the window functions assigns unique numbers each publisher price. based on outer order first display rows rn = 1 rows each publisher lowest price. second row each publisher has second lowest price , on.
sqlfiddle example: http://sqlfiddle.com/#!4/06ece/2
Comments
Post a Comment