我正在尝试对 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
我想要的是得到一行名为“总计”的行,然后是一月的总计,二月的总计等。我想我需要在某个地方进行求和,但无论我尝试什么似乎都会在错误的列中给出总计。我猜想也许我需要将整个查询包装在另一个查询中,但我似乎无法获得正确的语法。
如果我在选择中将 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
更新 13/02/2014 - 我现在已经解决了这个问题。我无法在单个查询中获得我想要的内容,因此,在这种情况下,我运行了上面的两个查询,并仅使用 PHP 将其全部显示为一张表。最终看来这是最简单的方法。
如果有人想知道我是如何做到这一点的,我会很乐意发布/发送我用来创建表并格式化它以显示来自多个查询的数据的代码。
最佳答案
我只是问了一个类似的问题,为什么不试试这个:
sum(case when year(`date_ready`) = '2013' then 1 else 0 end) as Total
关于mysql - 计数案例MYSQL中的总和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21627280/