我有一个关于如何将以下陈述分组为几个月的问题。
数据库是MYSQL
期望的结果是:
DRNAME, Jan, Feb, Mar, April, May ,June
SISHEN, 0, 0, 100, 250, 450, 500, 0
我得到的结果是:
DRNAME, Jan, Feb, Mar, April, May ,June
SISHEN, 0, 0, 100, 0, 0, 0,0
SISHEN, 0, 0,0,250,0,0,0
SISHEN, 0, 0, 0,0 , 450, 0, 0
查询是
select drname,
case when month(loaddate) = 1 then sum(drvalue) end as 'Jan',<br>
case when month(loaddate) = 2 then sum(drvalue) end as 'Feb',<br>
case when month(loaddate) = 3 then sum(drvalue) end as 'March',<br>
case when month(loaddate) = 4 then sum(drvalue) end as 'April',<br>
case when month(loaddate) = 5 then sum(drvalue) end as 'May',<br>
case when month(loaddate) = 6 then sum(drvalue) end as 'June'<br>
from tblloadschedule<br>
where cancelloadflag = 'N' and drname like 'sish%'<br>
group by drname,month(loaddate)
答案 0 :(得分:1)
您需要从month(loaddate)
子句中删除group by
,然后使用条件聚合:
select drname,
sum(case when month(loaddate) = 1 then drvalue else 0 end) as 'Jan',
sum(case when month(loaddate) = 2 then drvalue else 0 end) as 'Feb',
sum(case when month(loaddate) = 3 then drvalue else 0 end) as 'March',
sum(case when month(loaddate) = 4 then drvalue else 0 end) as 'April',
sum(case when month(loaddate) = 5 then drvalue else 0 end) as 'May',
sum(case when month(loaddate) = 6 then drvalue else 0 end) as 'June'
from tblloadschedule
where cancelloadflag = 'N' and drname like 'sish%'
group by drname
答案 1 :(得分:1)
修改您的查询以获取条件CASE
表达式的聚合:
SELECT
drname,
SUM(CASE WHEN MONTH(loaddate) = 1 THEN drvalue END) AS Jan,
SUM(CASE WHEN MONTH(loaddate) = 2 THEN drvalue END) AS Feb,
SUM(CASE WHEN MONTH(loaddate) = 3 THEN drvalue END) AS March,
SUM(CASE WHEN MONTH(loaddate) = 4 THEN drvalue END) AS April,
sum(CASE WHEN MONTH(loaddate) = 5 THEN drvalue END) AS May,
sum(CASE WHEN MONTH(loaddate) = 6 THEN drvalue END) AS June
FROM tblloadschedule
WHERE
cancelloadflag = 'N' AND drname LIKE 'sish%'
GROUP BY
drname;
答案 2 :(得分:0)
您不需要昂贵的案例:
select drname,
sum(if(month(loaddate) = 1 ,drvalue,0) as 'Jan',
sum(if(month(loaddate) = 2 , drvalue ,0) as 'Feb',
sum(if(month(loaddate) = 3 , drvalue ,0) as 'March',
sum(if(month(loaddate) = 4 , drvalue ,0) as 'April',
sum(if(month(loaddate) = 5 , drvalue ,0) as 'May',
sum(if(month(loaddate) = 6 , drvalue ,0) as 'June'
from tblloadschedule
where cancelloadflag = 'N' and drname like 'sish%'
group by drname
不使用IF编辑:
select drname,
sum((month(loaddate) = 1 ) * drvalue) as 'Jan',
sum((month(loaddate) = 2 ) * drvalue) as 'Feb',
sum((month(loaddate) = 3 ) * drvalue) as 'March',
sum((month(loaddate) = 4 ) * drvalue) as 'April',
sum((month(loaddate) = 5 ) * drvalue) as 'May',
sum((month(loaddate) = 6 ) * drvalue) as 'June'
from tblloadschedule
where cancelloadflag = 'N' and drname like 'sish%'
group by drname
答案 3 :(得分:0)
来自A. Colonna和IF的方法在服务器上确实效率更高,并返回了预期的结果。
非常感谢所有评论的人
我使用的代码是:
选择drname,
sum((month(loaddate)= 1)* drvalue)为'Jan',
sum((month(loaddate)= 2)* drvalue)为'Feb',
sum((month(loaddate)= 3)* drvalue)为'March',
sum((month(loaddate)= 4)* drvalue)为'April',
sum((month(loaddate)= 5)* drvalue)为'May',
sum((month(loaddate)= 6)* drvalue)为'June'
来自tblloadschedule
其中cancelloadflag ='N'
小组分组