Multiple COUNT SELECTS from the same table in one query

前端 未结 2 818
暗喜
暗喜 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:06
    SELECT sum(case when Page LIKE '%AEC%' then 1 end) as tAEC, 
        sum(case when Page LIKE '%TOL%' then 1 end) as tTOL 
    FROM someTable 
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题