开发者

count multiple in one statement

开发者 https://www.devze.com 2023-03-04 06:28 出处:网络
i have a table with data title and date of the data inserted. and right now i want to do count to make the statistic out of it.

i have a table with data title and date of the data inserted. and right now i want to do count to make the statistic out of it.

can i do multiple count in one sql statement? like from, the column date, i want to count how man开发者_JAVA百科y on this month, and how many in this year, until month selected. is it possible? if yes, how?

this is what i have come up, for now.

SELECT a.trigger_type_code
     , c.trigger_name
     , COUNT(*) AS number
FROM issue_trigger a 
  INNER JOIN cs_issue b
    ON b.issue_id = a.issue_id
  INNER JOIN ref_trigger_type c
    ON c.trigger_type_code = a.trigger_type_code
WHERE MONTH(b.created_date) = '05'
  AND YEAR(b.created_date) = '2011' 
GROUP BY trigger_type_code,trigger_name

by this is only for one count.help.:(


You could use a case:

select  sum(case when MONTH(b.created_date) = '05' 
            AND YEAR(b.created_date) = '2011' then 1 end) as Count_2011_05
,       sum(case when YEAR(b.created_date) = '2011'
            then 1 end) as Count_2011
from    ... etc ...


I think you could go like this:

SELECT
  a.trigger_type_code,
  c.trigger_name,
  COUNT(MONTH(b.created_date) < 5 OR NULL) AS before_the_month,
  COUNT(MONTH(b.created_date) = 5 OR NULL) AS in_the_month
FROM issue_trigger a 
  INNER JOIN cs_issue b
    ON b.issue_id = a.issue_id
  INNER JOIN ref_trigger_type c
    ON c.trigger_type_code = a.trigger_type_code
WHERE YEAR(b.created_date) = 2011
GROUP BY a.trigger_type_code, c.trigger_name
0

精彩评论

暂无评论...
验证码 换一张
取 消