日期 水库 水位DATE1 KU1 101
DATE1 KU2 201
. . .
. . .
. . .
DATE2 KU1 102
DATE2 KU2 202
. . .
. . .
. . .
DATE3 KU1 103
DATE3 KU2 203
. . .
. . .
. . .类似于上面的表,怎么写一个查询语句,得到一个记录集,使它变成下面的结构。谢谢了!日期 KU1 KU2 ...DATE1 101 201 ...
DATE2 102 202 ...
DATE3 103 203 ...
DATE1 KU2 201
. . .
. . .
. . .
DATE2 KU1 102
DATE2 KU2 202
. . .
. . .
. . .
DATE3 KU1 103
DATE3 KU2 203
. . .
. . .
. . .类似于上面的表,怎么写一个查询语句,得到一个记录集,使它变成下面的结构。谢谢了!日期 KU1 KU2 ...DATE1 101 201 ...
DATE2 102 202 ...
DATE3 103 203 ...
select distinct 日期 into #temp from 表declare @s_k varchar(50)
declare cur_1 cursor for
select distinct 水库 from 表
open cur_1
fetch cur_1 into @s_k
while @@fetch_status = 0
begin
exec('alter table #temp add [' + @s_k + '] decimal(9, 0) ')
exec("update a set a." + @s_k + " = sum(b.水位) from #temp a, 表 b where a.日期 = b.日期 and b.水库 = '"+@s_k+"'")
end
close cur_1
deallocate cur_1select * from #temp
drop table#temp
其实在PB中用crosstab就OK了
declare cur_1 cursor for
select distinct 水库 from 表
open cur_1
fetch cur_1 into @s_k
while @@fetch_status = 0
begin
exec('alter table #temp add [' + @s_k + '] decimal(9, 0) ')
exec("update a set a." + @s_k + " = sum(b.水位) from #temp a, 表 b where a.日期 = b.日期 and b.水库 = '"+@s_k+"'")
end
close cur_1
deallocate cur_1select * from #temp
drop table #temp
同意这种方式,我采用的就是这样的方式。
步骤:
首先创建一个临时表,通过游标进行表结构的修改,然后把数据插进去即可。
select 'DATE1' as 日期, 'KU1' as 水库, 101 as 水位
into test
union select 'DATE1', 'KU2', 201
union select 'DATE2', 'KU1', 102
union select 'DATE2', 'KU2', 202
union select 'DATE3', 'KU1', 103
union select 'DATE3', 'KU2', 203
-------------------------------------------
declare @s varchar(2000)
set @s = 'select 日期'
select @s = @s + ', sum(case 水库 when ''' + 水库 + ''' then 水位 end) as ' + 水库
from (select distinct 水库 from test) a
set @s = @s + ' from test group by 日期'
exec(@s)
/*
日期 KU1 KU2
DATE1 101 201
DATE2 102 202
DATE3 103 203
*/
--------------------------------------------
drop table test
create table tab1(日期 varchar(10),水库 varchar(10),水位 int)
insert tab1 select 'DATE1','KU1', 101
union all select 'DATE1','KU2',201
union all select 'DATE2','KU1',102
union all select 'DATE2','KU2',202--测试查询
declare @s varchar(1000)
select @s='select 日期'
select @s=@s+',['+水库+']=max(case 水库 when '''+水库+''' then 水位 end)' from tab1 group by 水库
exec(@s+' from tab1 group by 日期')
--删表表
drop table tab1
create table tab1(日期 varchar(10),水库 varchar(10),水位 int)
insert tab1 select 'DATE1','KU1', 101
union all select 'DATE1','KU2',201
union all select 'DATE2','KU1',102
union all select 'DATE2','KU2',202
go
--测试查询
declare @s varchar(1000)
select @s='select 日期'
select @s=@s+',['+水库+']=sum(case 水库 when '''+水库+''' then 水位 end)' from tab1 group by 水库
exec(@s+' from tab1 group by 日期')
go
--删表表
drop table tab1
go
(case 水库 when '''+水库+''' then 水位 end)
你把语句改成存储过程形式,调用就可以了,比交叉表灵活~如下:Create Proc sp_a
as
begin
declare @s varchar(1000)
select @s='select 日期'
select @s=@s+',['+水库+']=sum(case 水库 when '''+水库+''' then 水位 end)' from tab1 group by 水库
exec(@s+' from tab1 group by 日期')
end
go
--调用的时候
exec sp_a
select @s=@s+',['+水库+']=sum(case 水库 when '''+水库+''' then 水位 end)' from tab1 group by这样写有问题
Create Proc sp_a
as
begin
declare @s varchar(1000)
select @s='' --'select 日期' 写在后面
select @s=@s+',['+水库+']=sum(case 水库 when '''+水库+''' then 水位 end)' from tab1 group by 水库
exec('select 日期'+@s+' from tab1 group by 日期')
end
go