Multiple COUNT SELECTS from the same table in one query

前端 未结 2 819
暗喜
暗喜 2021-02-05 04:41

For some people it might look very simple, but I just simply can\'t get it.
I have multiple MS-SQL SELECT queries from the same table over and over:

         


        
2条回答
  •  别那么骄傲
    2021-02-05 05:07

    You can use a GROUP BY Page

    SELECT
      Page
    , COUNT(Page) as "nb"
    FROM someTable
    GROUP BY Page
    

    You can also do a GROUP BY CASE...

    SELECT
      CASE
        WHEN Page LIKE '%AEC%' THEN "AEC"
        WHEN Page LIKE '%TOL%' THEN "TOL"
        ELSE ""
      END AS "type"
    , count(Page) as "nb"
    FROM someTable
    GROUP BY type
    

    You can also COUNT IF

    SELECT
      COUNT(IF(Page LIKE '%AEC%', 1, NULL) THEN "AEC"
    , COUNT(IF(Page LIKE '%TOL%', 1, NULL) THEN "TOL"
    FROM someTable
    

提交回复
热议问题