select 日期,
       [长沙]=max(case 城市 when '长沙' then 温度 else 0 end),
       [湘潭]=max(case 城市 when '湘潭' then 温度 else 0 end),
       [株洲]=max(case 城市 when '株洲' then 温度 else 0 end)
from 表
group by 日期
order by 日期

解决方案 »

  1.   

    delcare @s varchar(8000)set @s='select 日期 '
    select @s=@s+',['+城市+']=max(case 城市 when '''+城市+''' then 温度 else 0 end)'
    from 表 
    group by 城市 
    set @s=@s+' from 表 group by 日期'exec(@s)
      

  2.   

    呵呵,谢谢小李兄弟,完全正确,
    这句:[长沙]=max(case 城市 when '长沙' then 温度 else 0 end),
    是什么意思?为什么要用max?
      

  3.   

    比如说在2004-12-21  这个日期分组里[长沙]=max(case 城市 when '长沙' then 温度 else 0 end)如果城市是长沙就显示对应的温度值 ,不是长沙就显示为0,这样用max函数取出最大的也就是长沙的了
      

  4.   

    就以长沙为例,这个max的意思是从哪几个值中取出最大的?
      

  5.   

    使用max主要是为了使用group by,呵呵
      

  6.   

    由于每个日期只有一条记录,你也可以用min,这里就是需要一个聚合函数,好按日期来做group by而已。
      

  7.   

    declare @s varchar(8000)
    set @s=''
    select @s=@s+',['+城市+']=case 城市 when '''+城市+''' then 溫度 else NULL end)'
    from 表 group by 城市
    exec('select 日期+@s+' from 表 group by 日期)
      

  8.   

    不好意思,寫錯一點點
    改正:(我測試過)
    declare @s varchar(8000)
    set @s=''
    select @s=@s+',['+城市+']=sum(case 城市 when '''+城市+''' then 溫度 else null end)'
    from 表  group by 城市
    exec('select 日期'+@s+' from 表 group by 日期')
      

  9.   

    create proc zcf_f1
      @prodId int
    as
      select prodName,prodId,bomNum
      from prod
      where prodId = @prodId
      

  10.   

    select @s=@s+',['+城市+']=max(case 城市 when '''+城市+''' then 温度 else 0 end)'为什么能变成:select 日期 ,[长沙      ]=max(case 城市 when '长沙      ' then 温度 else 0 end),[湘潭      ]=max(case 城市 when '湘潭      ' then 温度 else 0 end),[株洲      ]=max(case 城市 when '株洲      ' then 温度 else 0 end) 请兄弟们帮我解释一下吧,谢谢!
      

  11.   

    如果还有一个城市的对应表,我要显示出来为:
    -------------------------------------------------------------------------
    日期 | AA | BB | CC |
    -------------------------------------------------------------------------
    2004-12-21 | 23 | 28 | 26 |
    -------------------------------------------------------------------------
    2004-12-22 | 27 | 25 | 22 |
    -------------------------------------------------------------------------
    该如何办?请大家一定帮我...解决马上结贴.
      

  12.   

    问得好,奥妙就在select @s=@s 上,
    是一个字符串迭代,每次的@s都会增加一个种类的城市,
      

  13.   

    问得好,奥妙就在select @s=@s + ....上,
    是一个字符串迭代,每次的@s都会增加一个种类的城市,
      

  14.   

    --如果还有一个城市的对应表
    假设你的城市对应表名叫做城市对应表,里面有个字段叫城市 ,那就这样...
    delcare @s varchar(8000)
    set @s='select 日期 '
    select @s=@s+',['+城市+']=max(case 城市 when '''+城市+''' then 温度 else 0 end)'
    from (select distinct 城市 from 城市对应表) as a
    set @s=@s+' from 表 group by 日期'exec(@s)
      

  15.   

    sorry!,是有问题,你的记录温度的表和城市对应表的关联字段是什么?
      

  16.   

    select @s=@s+',['+城市+']=max(case 城市 when '''+城市+''' then 温度 else 0 end)'
    from (select distinct 城市 from 城市对应表) as a在城市对应表中取不到温度,所以不行
      

  17.   

    --不太清楚你的表和城市表的关系,猜猜看吧.delcare @s varchar(8000)
    set @s='select 日期 '
    select @s=@s+',['+城市+']=max(case 城市 when '''+城市+''' then 温度 else 0 end)'
    from (select distinct 城市 from (select b.日期,a.城市,b.温度 from 城市对应表 a join 表 b on a.城市=b.城市) a) as a
    set @s=@s+' from (select b.日期,a.城市,b.温度 from 城市对应表 a left join 表 b on a.城市=b.城市) a group by 日期'
    exec(@s)
      

  18.   

    To  浩子:
    declare @s varchar(8000)
    set @s=''
    select @s=@s+',['+城市+']=sum(case 城市 when '''+城市+''' then 溫度 else null end)'
    from 表  group by 城市
    exec('select 日期'+@s+' from 表 group by 日期')
    第三行是select @s=@s+',['+城市+']=sum(case 城市 when '''+城市+''' then 溫度 else null end)'你貼的卻是select @s=@s+',['+城市+']=max(case 城市 when '''+城市+''' then 溫度 else null end)'
      

  19.   

    delcare @s varchar(8000)
    set @s='select 日期 '
    select @s=@s+',['+城市+']=sum(case 城市 when '''+城市+''' then 温度 else 0 end)'
    from (select distinct 城市 from 城市对应表) as a
    set @s='select 日期'+@s+' from 表 group by 日期'exec(@s)