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

非专业开发Five 2012-07-02 02:46:18
[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的和显示为一个字段。我做了很久都没有头绪,各位大牛给个方法吧,谢谢了!
...全文
135 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
谢谢各位,结贴给分~
  • 打赏
  • 举报
回复

with t
as(
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
)
select [日期],sid,[08时],......,[05时],([r1]+[r2]+[r3]+[r4]) as rtotal from t

筱筱澄 2012-07-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

但是要求和的,2楼那个能求和么?
[/Quote]
max 改成sum 就是求和
你要最大的四个和吗?
下面的就可以


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

  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

--显示为一个字段,是下面的意思吗?
SQL code
r=max(case when MicapsDm.times in ('02' ,'08','14','20') then cast(MicapsDm.Rr as int) else 0 end)
[/Quote]
的“和”显示为一个字段,要求和。你这个不能求和的
  • 打赏
  • 举报
回复
但是要求和的,2楼那个能求和么?
  • 打赏
  • 举报
回复
哇,就是2楼的意思,我实验看看通过了就给分~~~
筱筱澄 2012-07-02
  • 打赏
  • 举报
回复
--显示为一个字段,是下面的意思吗?
r=max(case when MicapsDm.times in ('02' ,'08','14','20') then cast(MicapsDm.Rr as int) else 0 end)
  • 打赏
  • 举报
回复

--> 测试数据:[tbl]
if object_id('[tbl]') is not null drop table [tbl]
create table [tbl]([xrq] datetime,[rgl] int,[jcmc] varchar(12))
insert [tbl]
select '2012-04-01',4182,'51路' union all
select '2012-04-01',598,'机2(7)' union all
select '2012-04-01',2520,'711路' union all
select '2012-04-02',258,'宝山社区罗罗' union all
select '2012-04-02',5092,'淞安专线' union all
select '2012-04-02',504,'1211路' union all
select '2012-04-02',778,'302路' union all
select '2012-04-03',2515,'彭罗专线' union all
select '2012-04-03',3852,'51路' union all
select '2012-04-03',148,'机2(7)' union all
select '2012-04-03',1930,'711路' union all
select '2012-04-03',50,'特约二'



declare @str varchar(max)
set @str=''
select @str=@str+',['+replace(convert(varchar(10),[xrq],120),'-','')+']'+
'=sum(case when [xrq]='+quotename(replace(convert(varchar(10),[xrq],120),'-','')
,'''')+' then [rgl] else 0 end)'
from tbl group by replace(convert(varchar(10),[xrq],120),'-',''),[xrq]
exec('select isnull([jcmc],''竖向求和'') as xrq'+@str
+',sum(rgl) as [横向和] from [tbl] group by [jcmc] with rollup')

/*
xrq 20120401 20120402 20120403 横向和
1211路 0 504 0 504
302路 0 778 0 778
51路 4182 0 3852 8034
711路 2520 0 1930 4450
宝山社区罗罗 0 258 0 258
机2(7) 598 0 148 746
彭罗专线 0 0 2515 2515
淞安专线 0 5092 0 5092
特约二 0 0 50 50
竖向求和 7300 6632 8495 22427
*/

--用这个方法试试

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧