【发布时间】:2015-10-25 17:08:38
【问题描述】:
我想要按日历季度分组的表格一列的总和。更具体地说,我现在拥有的是逐月想要的列的总和:
select month(emitido_date) as mes, year(emitido_date) as ano, ifnull(sum((det.preco * det.quantidade) * (iva.valor/100)),0) as totaliva
from documento as doc
inner join documento_serie as serie on serie.id = doc.documento_serie_id
inner join documento_detail as det on doc.id = det.documento_id
inner join phos_iva as iva on iva.id = det.iva_id
where serie.documento_categoria_id = 2 or serie.documento_categoria_id = 3
group by mes, ano
order by mes, ano desc
上面的代码很好,对我有用。它返回我想要按月分组的列的总和。例如:1 月: 100,2 月: 200,3 月: 300。直到 12 月为止。
我的问题是如何按日历季度分组。具体来说,寻找上面的例子,现在我想创建一个查询,它给我 [January, February, March]=600, [April, May, June], [July, August, September] 的值的总和]、[10 月、11 月、12 月]。我尝试使用下面的代码,但出现错误:
1111 - 组函数的使用无效。
select
sum(case when month(emitido_date) in (1,2,3) then ifnull(sum(det.quantidade),0) else 0 end) as Tri1,
sum(case when month(emitido_date) in (4,5,6) then ifnull(sum(det.quantidade),0) else 0 end) as Tri2,
select
sum(case when month(emitido_date) in (1,2,3) then ifnull(sum((det.preco * det.quantidade) * (iva.valor/100)),0) else 0 end) as Tri1,
sum(case when month(emitido_date) in (4,5,6) then ifnull(sum((det.preco * det.quantidade) * (iva.valor/100)),0) else 0 end) as Tri2,
sum(case when month(emitido_date) in (7,8,9) then ifnull(sum((det.preco * det.quantidade) * (iva.valor/100)),0) else 0 end) as Tri3,
sum(case when month(emitido_date) in (10,11,12) then ifnull(sum((det.preco * det.quantidade) * (iva.valor/100)),0) else 0 end) as Tri4,
year(emitido_date) as ano
from documento as doc
inner join documento_serie as serie on serie.id = doc.documento_serie_id
inner join documento_detail as det on doc.id = det.documento_id
inner join phos_iva as iva on iva.id = det.iva_id
where serie.documento_categoria_id = 3
group by year(emitido_date)
怎么了?
【问题讨论】:
-
我不知道 3 in 3 months 是什么意思。如果您愿意,请考虑遵循这个简单的两步操作: 1. 如果您还没有这样做,请提供适当的 DDL(和/或 sqlfiddle),以便我们可以更轻松地复制问题。 2. 如果您尚未这样做,请提供与步骤 1 中提供的信息相对应的所需结果集。
-
“3个月内3个”是指以下期间一列的总和:[Jan,mar],[apr,jun],[jul,sept],[oct,dec]
-
"3 in 3" 不是你用英语怎么说的。在这种情况下,它翻译为“3 by 3 个月”,很难在句子中与“group by”结合使用。更好的说法可能是“按季度分组”。
-
对不起我的英语。我已经编辑了问题。
标签: mysql sql group-by aggregate-functions mysql-error-1111