我试图在Count Case查询中累计列。我有以下查询
SELECT distinct type,
Year(`date_ready`) as Year,
Count(case when month(`date_ready`)=1 then `type` end) As Jan_Attr,
Count(case when month(`date_ready`)=2 then `type ` end) As Feb_Attr,
Count(case when month(`date_ready`)=3 then `type ` end) As Mar_Attr,
Count(case when month(`date_ready`)=4 then `type ` end) As Apr_Attr,
Count(case when month(`date_ready`)=5 then `type ` end) As May_Attr,
Count(case when month(`date_ready`)=6 then `type ` end) As Jun_Attr,
Count(case when month(`date_ready`)=7 then `type ` end) As Jul_Attr,
Count(case when month(`date_ready`)=8 then `type ` end) As Aug_Attr,
Count(case when month(`date_ready`)=9 then `type ` end) As Sep_Attr,
Count(case when month(`date_ready`)=10 then `type ` end) As Oct_Attr,
Count(case when month(`date_ready`)=11 then `type ` end) As Nov_Attr,
Count(case when month(`date_ready`)=12 then `type ` end) As Dec_Attr
FROM newsuit
GROUP BY Year(`date_ready`), type
我想要的是获得一个名为Total的行,然后是Jan的总数,2月的总数等等。我认为我需要在某个地方使用Sum,但无论我尝试什么似乎都给了我错误列中的总数。我猜我可能需要将整个查询包装在另一个中,但我似乎无法正确理解语法。
如果我在选择中将Sum(type)作为Total,我只得到一个包含总数的单元格。我尝试过的所有其他内容都会给我错误。
...干杯
其他发现。我发现在From子句之前添加以下内容会为每行添加总计。这很有用,但是我仍然希望找到列的总数:)
sum(case when month(`date_ready`) <13 then 1 else 0 end) as Total
进一步发现......
这为我提供了列总数,只需要合并两个查询。
Select Sum(PQ.Jan_Attr) As JanTot, Sum(PQ.Feb_Attr) As FebTot,Sum(PQ.Mar_Attr) As MarTot,Sum(PQ.Apr_Attr) As AprTot,Sum(PQ.May_Attr) As MayTot,Sum(PQ.Jun_Attr) As JunTot,
Sum(PQ.Jul_Attr) As JulTot,Sum(PQ.Aug_Attr) As AugTot,Sum(PQ.Sep_Attr) As SepTot,Sum(PQ.Oct_Attr) As OctTot,Sum(PQ.Nov_Attr) As NovTot ,Sum(PQ.Dec_Attr) As DecTot
From
(SELECT distinct type,
Year(`date_ready`) as Year,
Count(case when month(`date_ready`)=1 then `type` end) As Jan_Attr,
Count(case when month(`date_ready`)=2 then `type` end) As Feb_Attr,
Count(case when month(`date_ready`)=3 then `type` end) As Mar_Attr,
Count(case when month(`date_ready`)=4 then `type` end) As Apr_Attr,
Count(case when month(`date_ready`)=5 then `type` end) As May_Attr,
Count(case when month(`date_ready`)=6 then `type` end) As Jun_Attr,
Count(case when month(`date_ready`)=7 then `type` end) As Jul_Attr,
Count(case when month(`date_ready`)=8 then `type` end) As Aug_Attr,
Count(case when month(`date_ready`)=9 then `type` end) As Sep_Attr,
Count(case when month(`date_ready`)=10 then `type` end) As Oct_Attr,
Count(case when month(`date_ready`)=11 then `type` end) As Nov_Attr,
Count(case when month(`date_ready`)=12 then `type` end) As Dec_Attr,
sum(case when month(`date_ready`) <13 then 1 else 0 end) as Total
FROM newsuit
WHERE Year(`date_ready`) = '2013'
GROUP BY Year(`date_ready`), type) as PQ
更新2014年2月13日 - 我现在已经解决了这个问题。我无法在单个查询中得到我想要的东西,所以在这个事件中,我运行了上面的两个查询,只是使用PHP将它们全部显示为一个表。似乎最终是最简单的方式。
如果有人想知道我是怎么做到的,我会很乐意发布/发送用于创建表格的代码并将其格式化以显示来自多个查询的数据。
答案 0 :(得分:1)
I was just asking a similar question, why not try this:
sum(case when year(`date_ready`) = '2013' then 1 else 0 end) as Total
答案 1 :(得分:0)
怎么样
SELECT
suit_type,
Year(`date_ready`) as Year,
SUM(IF(month(`date_ready`) = 1,`type`,0)) AS Jan_Attr,
...
FROM newsuit
GROUP BY Year(`date_ready`), suit_type