这样的SQL语句怎么写?解决后立刻给分!

cshmai 2006-04-20 03:54:02
日期 水库 水位

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 ...

...全文
270 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
hexi0917 2006-04-25
  • 打赏
  • 举报
回复
mark..
liangpei2008 2006-04-20
  • 打赏
  • 举报
回复
UP
撸大湿 2006-04-20
  • 打赏
  • 举报
回复
to itblog(i like i do)

select @s='select 日期'
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
regithanhu 2006-04-20
  • 打赏
  • 举报
回复
学习
itblog 2006-04-20
  • 打赏
  • 举报
回复
用不着使交叉表啊~
你把语句改成存储过程形式,调用就可以了,比交叉表灵活~如下:

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
edp08 2006-04-20
  • 打赏
  • 举报
回复
对,不用求和,sum是我习惯性加上去的

dulei115 2006-04-20
  • 打赏
  • 举报
回复
楼上的,不关求和的事,LZ的这个用sum,max,min,avg的结果都一样,因为括号里面就对应每个日期一个满足条件:
(case 水库 when '''+水库+''' then 水位 end)
cshmai 2006-04-20
  • 打赏
  • 举报
回复
谢谢大家了,不知道用交叉表能不能实现
itblog 2006-04-20
  • 打赏
  • 举报
回复
忘记是求和了,更正一下:

--测试表
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
itblog 2006-04-20
  • 打赏
  • 举报
回复
--测试表
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
dulei115 2006-04-20
  • 打赏
  • 举报
回复
if object_id('test') is not null drop table test
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
  • 打赏
  • 举报
回复
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_1

select * from #temp
drop table #temp
同意这种方式,我采用的就是这样的方式。
步骤:
首先创建一个临时表,通过游标进行表结构的修改,然后把数据插进去即可。
edp08 2006-04-20
  • 打赏
  • 举报
回复
水库多少都没问题,把上面汉字换成你的表名和字段名即可
edp08 2006-04-20
  • 打赏
  • 举报
回复

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_1

select * from #temp
drop table#temp


其实在PB中用crosstab就OK了
cshmai 2006-04-20
  • 打赏
  • 举报
回复
谢谢你,老板让我用VB做,将结果显示在flexgrrid上,而且水库还不是固定的,所以在程序中还要控制水库的多少
btlyeo 2006-04-20
  • 打赏
  • 举报
回复
我也遇见过这样的问题,也想学习一下~

不过我们在ASP页面里解决了这个问题,用的filter过滤器,如果有需要可以给你例子

34,576

社区成员

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

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