当前位置: 代码迷 >> 报表 >> SQL语句不能正确显示结果,该如何解决
  详细解决方案

SQL语句不能正确显示结果,该如何解决

热度:615   发布时间:2016-05-05 08:13:01.0
SQL语句不能正确显示结果
有以下一张临时表
DEPT_NO MFC_DEPT WSC_NO SAM CREATE_DATE 
CCIT 1 CM 12 2010-01-07 
CCIT 1 CM 2 2010-01-08 
CCIT 1 ES 1.5 2010-01-08 
CCIT 1 ES 2 2010-01-09 

我想要的结果是CM在一行显示 日期为7为一个字段为8时也为一个字段,但是以下语句显示不正确日期为8时sam为0,
请达人帮忙指正,谢谢


select wsc_no,mfc_dept,(case when day(a.create_date)=7 then a.sam else 0 end) '7',
  (case when day(a.create_date)=8 then a.sam else 0 end) '8', 
  (case when day(a.create_date)=9 then a.sam else 0 end) '9' 
from 
(
select a.dept_no,mfc_dept,wsc_no,sum(fact_hour) sam,b.create_date from sys_dept a,mfc_course b  
where date_format(b.create_date,'%y%m')=1001 
and a.dept_id=b.mfc_dept 
group by wsc_no,create_date 
) a,sys_dept b 
group by wsc_no

------解决方案--------------------
仔细看看我的代码
select wsc_no,mfc_dept,sum(case when day(a.create_date)=7 then a.sam else 0 end) '7',
sum(case when day(a.create_date)=8 then a.sam else 0 end) '8',
sum(case when day(a.create_date)=9 then a.sam else 0 end) '9'
from
(
select a.dept_no,mfc_dept,wsc_no,sum(fact_hour) sam,b.create_date from sys_dept a,mfc_course b
where date_format(b.create_date,'%y%m')=1001
and a.dept_id=b.mfc_dept
group by wsc_no,create_date
) a,sys_dept b
group by wsc_no
  相关解决方案