计数情况下的列总和MYSQL

计数情况下的列总和MYSQL,mysql,Mysql,我正在尝试合计计数案例查询中的列。我有以下疑问 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 t

我正在尝试合计计数案例查询中的列。我有以下疑问

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
我想要的是得到一个名为“总计”的行,然后是1月的总计,2月的总计,等等。我想我需要一个总数,但无论我尝试什么,似乎都会在错误的列中给出总计。我猜可能我需要将整个查询包装在另一个查询中,但我似乎无法正确理解语法

如果我把Sumtype作为Total放在select中,我只会得到一个单元格,其中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将其全部显示为一个表。最后似乎是最简单的方法

如果有人想知道我是如何做到这一点的,我很乐意发布/发送我用来创建表的代码,并将其格式化以显示来自多个查询的数据。

怎么样

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

我只是问了一个类似的问题,为什么不试试这个:

sum(case when year(`date_ready`) = '2013' then 1 else 0 end) as Total

试着用一个小组怎么样query@bjhaid听起来像个计划,能给我总数吗?谢谢你的迅速回复。我得到以下错误'1064-您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以获得正确的语法,以便使用near'AS Jan_Attr,SUMIFmonthdate_ready=2,键入,0 AS Feb_Attr,SUMIFm'在第3行'……是的,我错过了一个。现在试试这一个没有错误,但也没有给出正确的输出。我得到了正确的列标题和行名称,即带有月份的列标题,以及带有“type”的不同值的行,但是总计不显示在列的底部,各个月份的值也不显示在单元格中。感谢您的帮助: