SQL Query: Need order by count, most must be on top, the rest follows

前端 未结 3 1388
孤街浪徒
孤街浪徒 2021-02-09 20:14

TABLEA

JobCode Job1 Job2 Job3 zip
------- ---- ---- ---- ----------
F       F    S    NULL 90030
F       F    S    NULL 90031
F       F    S    NULL 90031
F              


        
3条回答
  •  时光取名叫无心
    2021-02-09 20:25

    SQL Server 2008 using COUNT() OVER

    select *, c = count(1) over (partition by zip)
    from tbl
    order by c desc;
    

    If you don't need to see the additional column, then you can move the COUNT() OVER clause into the ORDER BY clause.

    select JobCode, Job1, Job2, Job3, zip
    from tbl
    order by count(1) over (partition by zip) desc;
    

提交回复
热议问题