I have the following table:
TKG hour CAP SWITCH CALLS
AAA 7 45 HH 56
AAA 8 35 HH 76
AAA 9 25 HH 43
BBB 7 32 LL 5
BBB 8 43 LL 65
BBB 9 434 LL 65
CCC 7 54 JJ 43
CCC 8 564 JJ 43
CCC 9 54 JJ 65
ddd 7 10 MM 4
ddd 8 10 MM 3
ddd 9 10 MM 5
I need to order by the TKG with the max CAP so the output will look like this:
TKG hour CAP SWITCH CALLS
AAA 7 45 HH 56
BBB 9 434 LL 65
CCC 8 564 JJ 43
ddd 7 10 MM 4
How can I do that?
If you are using Sql server 2005+, then you can use
row_number()andpartition by TKGandorder by cap desc:See SQL Fiddle with Demo
Result: