在一个语句中计算多个

时间:2011-05-08 08:15:42

标签: mysql sql vb.net

我有一个表格,其中包含数据标题和插入数据的日期。 而且现在我想做些计算来统计它。

我可以在一个sql语句中进行多次计数吗? 从列,日期开始,我想计算这个月有多少,以及今年有多少,直到月份选中。可能吗?如果有,怎么样?

这就是我现在提出来的。

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

这只是一个count.help。:(

2 个答案:

答案 0 :(得分:1)

您可以使用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 ...

答案 1 :(得分:0)

我想你可以这样:

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