| SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it. |
|
|||
Complicated sorting in one sqlHello,
I am trying to write a query that returns this result set: Table “Tests”: Code:
Id Test 1 B 2 B 3 C 4 A 5 B Code:
Id Test 1 B 2 B 5 B 3 C 4 A I have come up with this:
SELECT Id, Test,The result set groups the tests by name as needed, but the order is by test, not by id... any ides? 10x, Alex |
|
|||
|
no, i cant use ORDER BY because the results are not ordered
by Id or by Test. They are grouped (for display) by Test but ordered by Id. i.e.: Test "B" willl apear first because it has the smallest Id. Since the results are grouped by Test all the rest of the B's will appear directly after it. Then the groupd of Test "A", and last the group of Test "C" 10x, Alex |
|
|||
|
This should be easy (if I understand correctly):
1) select the groups the and the minimum for each group (as table T) 2) order T by id (as table T1) 3) select from T1 join Tests on Tests.test = T1.test Derived tables in sub-selects. Do you need more detailed help? Did it work? |