想问下这段sql应该如何优化,还有更好的做法吗?
sql语句如下
SELECT sum(case when CONVERT(varchar(100), b.BillDate, 112) >= 20180101 AND CONVERT(varchar(100), b.BillDate, 112) < 20180126 then b.totalmoney else 0 end)AS '一月',
sum(case when CONVERT(varchar(100), b.BillDate, 112) >= 20180126 AND CONVERT(varchar(100), b.BillDate, 112) < 20180226 then b.totalmoney else 0 end)AS '二月',
sum(case when CONVERT(varchar(100), b.BillDate, 112) >= 20180226 AND CONVERT(varchar(100), b.BillDate, 112) < 20180326 then b.totalmoney else 0 end)AS '三月',
sum(case when CONVERT(varchar(100), b.BillDate, 112) >= 20180326 AND CONVERT(varchar(100), b.BillDate, 112) < 20180426 then b.totalmoney else 0 end)AS '四月',
sum(case when CONVERT(varchar(100), b.BillDate, 112) >= 20180426 AND CONVERT(varchar(100), b.BillDate, 112) < 20180526 then b.totalmoney else 0 end)AS '五月',
sum(case when CONVERT(varchar(100), b.BillDate, 112) >= 20180526 AND CONVERT(varchar(100), b.BillDate, 112) < 20180626 then b.totalmoney else 0 end)AS '六月',
sum(case when CONVERT(varchar(100), b.BillDate, 112) >= 20180626 AND CONVERT(varchar(100), b.BillDate, 112) < 20180726 then b.totalmoney else 0 end)AS '七月',
sum(case when CONVERT(varchar(100), b.BillDate, 112) >= 20180726 AND CONVERT(varchar(100), b.BillDate, 112) < 20180826 then b.totalmoney else 0 end)AS '八月',
sum(case when CONVERT(varchar(100), b.BillDate, 112) >= 20180826 AND CONVERT(varchar(100), b.BillDate, 112) < 20180926 then b.totalmoney else 0 end)AS '九月',
sum(case when CONVERT(varchar(100), b.BillDate, 112) >= 20180926 AND CONVERT(varchar(100), b.BillDate, 112) < 20181026 then b.totalmoney else 0 end)AS '十月',
sum(case when CONVERT(varchar(100), b.BillDate, 112) >= 20181026 AND CONVERT(varchar(100), b.BillDate, 112) < 20181126 then b.totalmoney else 0 end)AS '十一月',
sum(case when CONVERT(varchar(100), b.BillDate, 112) >= 20181126 AND CONVERT(varchar(100), b.BillDate, 112) < 20181231 then b.totalmoney else 0 end)AS '十二月'
FROM BillIndex as b WHERE
datepart(year,b.BillDate)='2018'
AND BillType = 11
AND ifcheck = 't'
AND RedWord = 0
AND Stypeid = '00001';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论