SQL server - order by group after you find highest count -
how order second column based on primary sorted. lets say...
select customer, status, count(*) qty inventory group customer, status order count(*) desc
returns
customer | status | qty ------------------------------- 102 | 2 | 500 101 | 1 | 400 102 | 1 | 300 101 | 2 | 200 102 | 3 | 100
how can group customer after qty sorted? want qty primary sort , customer secondary.
customer | status | qty ------------------------------- 102 | 2 | 500 102 | 1 | 300 102 | 3 | 100 101 | 1 | 400 101 | 2 | 200
thanks!
edit: forgot desc after count(*)
you can use approach:
create table #mytemptable ( customer int, mystatus int, qty int) insert #mytemptable select customer, status, count(*) inventory group customer, status select * #mytemptable order qty desc, customer
Comments
Post a Comment