专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 报表

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

发布时间:2010-06-13 14:07:30 文章来源:www.iduyao.cn 采编人员:星星草
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
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: