mysql - 计数案例MYSQL中的总和列

标签 mysql

我正在尝试对 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/

相关文章:

java - Hibernate 进行正确的数据库查询但返回空实体

php - 将从 MySQL 检索的日期转换为请求的格式 (mm/dd/yy)

mysql - 获取mysql中每组的最后一行

php - 选择与特定日期重叠的日期范围

MySQL (MariaDB) float 可视化 : why are values rounded to hundreds or thousands?

mysql - 如何左连接 2 个不同数据库上的 2 个表?

php - 如何防止 PHP 中的 SQL 注入(inject)?

mysql正则表达式获取精确的行集

mysql - 修复连接的多对多 MySQL 查询

php - 允许用户搜索 MySQL 数据库时处理不同 SQL 搜索查询的最佳方法