Friday, 23 August 2013
Nested order by/order by within order by in SQL
Nested order by/order by within order by in SQL References I am looking to sort my sql results via an sql query with presumably some sort of nested order by/order by within an order by clause I have the following data: TERM USER ITEM_NO SCORE man sam 2 NULL man sam 1 170 man jerry 1 100 man jerry 2 NULL man sam 3 NULL and I wish to obtain the following order for the results: TERM USER ITEM_NO SCORE man sam 1 170 man sam 2 NULL man sam 3 NULL man jerry 1 100 man jerry 2 NULL The results must be sorted firstly by the score (stored only in item_no 1 for each user) descending. However any further items created by that user for the seleted term must also be picked up and inserted directly following and in item_no order. My current query looks like this: SELECT * FROM table WHERE term = \'man\' ORDER BY SCORE DESC, ITEM_NO ASC ...however this simply results as follows: TERM USER ITEM_NO SCORE man sam 1 170 man jerry 1 100 man sam 2 NULL man jerry 2 NULL man sam 3 NULL Thank you for any suggestions.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment