日期   水库   水位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  ...

解决方案 »

  1.   

    我也遇见过这样的问题,也想学习一下~不过我们在ASP页面里解决了这个问题,用的filter过滤器,如果有需要可以给你例子
      

  2.   

    谢谢你,老板让我用VB做,将结果显示在flexgrrid上,而且水库还不是固定的,所以在程序中还要控制水库的多少
      

  3.   


    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了
      

  4.   

    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
    同意这种方式,我采用的就是这样的方式。
    步骤:
    首先创建一个临时表,通过游标进行表结构的修改,然后把数据插进去即可。
      

  5.   

    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
      

  6.   

    --测试表
    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
      

  7.   

    忘记是求和了,更正一下:--测试表
    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
      

  8.   

    楼上的,不关求和的事,LZ的这个用sum,max,min,avg的结果都一样,因为括号里面就对应每个日期一个满足条件:
    (case 水库 when '''+水库+''' then 水位 end)
      

  9.   

    对,不用求和,sum是我习惯性加上去的
      

  10.   

    用不着使交叉表啊~
    你把语句改成存储过程形式,调用就可以了,比交叉表灵活~如下: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
      

  11.   

    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