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

前端 未结 3 1383
孤街浪徒
孤街浪徒 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:41

    To accomplish this, you should join against a subquery which returns the count per zipcode. The joined subquery is only needed to provide the counts (even if not displayed), while the main table yourtable provides all the rest of the columns.

    SELECT 
      JobCode, 
      Job1,
      Job2,
      Job3,
      subq.zip
    FROM
      yourtable
      JOIN (
         /* Subquery returns count per zip group */
         SELECT zip, COUNT(*) AS numzip 
         FROM yourtable 
         GROUP BY zip
      ) subq ON yourtable.zip = subq.zip
    ORDER BY numzip DESC
    

提交回复
热议问题