还是动态行转列的问题,这次涉及到求和、取值等问题了“我又来sql区100分敬上系列”

非专业开发Five 非专业开发Five 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
[sid] [yyyymmdd] [times] [Cc] [Wd] [Ws] [Pr] [Prc3] [Pwe] [Rr] [Cls] [Clc] [Clh] [Dev] [Vis] [We] [Tp] [Cms] [Chs] [Prc24]

01001 20120629 11 7 360 1 30 4 0 0 9999 34 6 1500 11.7 30.0 0 27.3 20 2 9999
01001 20120630 14 7 340 1 56 -3 8 0 .01 34 5 1500 11.6 30.0 25 24.6 20 2 -5
01001 20050702 20 6 40 2 23 13 0 0 0 39 2 1500 4.9 30.0 0 28.7 20 2 0
01001 20120628 05 0 200 3 4 0 0 0 9999 30 0 9999 9.1 30.0 0 24.3 20 2 2
01001 20120629 23 7 110 2 36 5 0 0 9999 34 6 1500 12.1 30.0 0 24.5 20 2 9999
01001 20120630 02 7 250 1 27 -5 8 0 .01 34 6 1500 12.9 30.0 80 24.3 20 2 -2
01001 20120630 17 7 360 2 36 -18 8 0 9999 34 4 1500 11.7 30.0 0 25.9 20 2 -3
01001 20120701 02 7 250 3 77 13 0 0 .3 39 6 1500 14.9 30.0 95 19.9 20 2 -4
01001 20050703 14 1 290 4 26 -11 0 0 0 31 1 1000 6.7 30.0 0 28.7 20 2 21.7
01001 20050704 02 2 200 2 31 0 0 0 0 34 2 1500 7.6 30.0 0 25.0 20 2 1
01001 20050704 05 0 200 3 30 -1 0 0 9999 9999 0 9999 7.2 30.0 0 22.6 9999 2 31.7
01001 20110704 11 0 70 1 131 -4 0 0 9999 9999 0 9999 4.7 30.0 0 23.2 9999 2 9999
01001 20110704 14 0 40 3 114 -13 0 0 0 9999 0 9999 5.1 30.0 0 25.5 9999 2 7
01001 20110704 17 1 360 3 104 -9 0 0 9999 31 1 1000 6.4 30.0 0 26.3 20 2 5
01001 20110704 20 0 20 2 97 -6 0 0 0 9999 0 9999 6.1 30.0 0 25.4 9999 2 5
01001 20110704 23 0 90 1 97 2 0 0 9999 9999 0 9999 8.7 30.0 0 20.7 9999 2 9999
01001 20110705 02 0 200 4 89 -4 0 0 0 9999 0 9999 8.8 30.0 0 20.1 9999 2 3
01001 20110705 05 0 200 2 87 -3 0 0 9999 9999 0 9999 8.5 30.0 0 18.8 9999 2 3
01001 20110705 08 0 220 2 93 4 0 0 0 9999 0 9999 10.3 30.0 0 18.5 9999 2 2
01001 20110705 11 0 40 2 85 -7 0 0 9999 9999 0 9999 11.5 30.0 0 23.4 9999 2 9999
01001 20110208 08 8 90 1 221 2 7 0 .5 35 2 1500 -8.4 15.0 70 -7.2 22 2 -1


以上是表字段和一些值,所有字段nvchar是我的特点,不是我想这么做,用户的数据就是这样的。
我现在需要的是一些动态行转列和分类汇总、求和等。
我自己根据前一篇帖子已经完成了动态行转列,但是在汇总求和和字段拼接上出了问题
放上我自己写的sql,等待大家的修改。


SELECT micapsdm.yyyymmdd as [日期]
,micapsdm.sid
,[08时]=max(case when MicapsDm.times='08' then MicapsDm.cms else null end)
,[11时]=max(case when MicapsDm.times='11' then MicapsDm.cms else null end)
,[14时]=max(case when MicapsDm.times='14' then MicapsDm.cms else null end)
,[17时]=max(case when MicapsDm.times='17' then MicapsDm.cms else null end)
,[20时]=max(case when MicapsDm.times='20' then MicapsDm.cms else null end)
,[23时]=max(case when MicapsDm.times='23' then MicapsDm.cms else null end)
,[02时]=max(case when MicapsDm.times='02' then MicapsDm.cms else null end)
,[05时]=max(case when MicapsDm.times='05' then MicapsDm.cms else null end)


,[r1]=max(case when MicapsDm.times='02' then cast(MicapsDm.Rr as int) else 0 end)
,[r2]=max(case when MicapsDm.times='08' then cast(MicapsDm.Rr as int) else 0 end)
,[r3]=max(case when MicapsDm.times='14' then cast(MicapsDm.Rr as int) else 0 end)
,[r4]=max(case when MicapsDm.times='20' then cast(MicapsDm.Rr as int) else 0 end)


FROM MicapsDm
group by micapsdm.yyyymmdd,micapsdm.sid
order by micapsdm.sid,micapsdm.yyyymmdd



现在需要求出r1、r2、r3、r4的和显示为一个字段。我做了很久都没有头绪,各位大牛给个方法吧,谢谢了!
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
非专业开发Five
MS-SQL Server 34,876 成员 254,635 内容