I need help with SQL (MSSQL), where I am sorting by multiple boolean types in order to group the results, but also need to sort by a date field within each of the type groupings.
The following SQL seemingly works ok:
Select * from staff ORDER BY admin DESC, hr DESC, sales DESC, it DESC, updated DESC
The trick is that some records have more than 1 type selected and then the output gets messed up. I only need to show staff in one grouping, but they should appear sorted by updated DESC within each grouping.
This is the output I get:
Name updated admin hr sales it
-------------------------------------------
fred 2012/04/01 true true
bill 2011/10/01 true true
joe 2012/04/01 true
sam 2012/03/01 true
jo 2012/02/01 true
beth 2012/03/01 true
mary 2012/02/01 true
harry 2011/02/01 true true
gary 2012/04/01 true
bruce 2012/04/01 true
This is the output I need
Name updated admin hr sales it
-------------------------------------------
fred 2012/04/01 true true
joe 2012/04/01 true
sam 2012/03/01 true
jo 2012/02/01 true
bill 2011/10/01 true true
beth 2012/03/01 true
mary 2012/02/01 true
gary 2012/04/01 true
harry 2011/02/01 true true
bruce 2012/04/01 true
Hope that makes sense.
You can use case expressions in the order by clause for conditional sorting.