select 日期,max(case 时间 where '上午' then 温度 else 0 end) as 上午温度,
max(case 时间 where '上午' then 湿度 else 0 end) as 上午湿度,
max(case 时间 where '下午' then 温度 else 0 end) as 下午温度,
max(case 时间 where '下午' then 湿度 else 0 end) as 下午湿度,
max(最高温度) as 最高温度,
min(最低湿度) as 最低湿度,
max(记录人) as 记录人
 from 表 group by 日期

解决方案 »

  1.   

    select 日期,sum(case 时间 when '上午' then 温度 else 0 end) as 上午温度,
           sum(case 时间 when '上午' then 湿度 else 0 end) as 上午湿度,
           sum(case 时间 when '下午' then 温度 else 0 end) as 下午温度,
           sum(case 时间 when '下午' then 湿度 else 0 end) as 下午湿度,
           max(最高温度) as 最高温度,max(最低湿度) as 最低湿度,
           max(记录人) as 记录人
    from 表 group by 日期
      

  2.   

    楼主,你给你数据好像有问题数据:
        日期  时间   温度  湿度  最高温度  最高湿度 记录人
          1   上午    10    20    30         50    a
          1   下午    25    60    35         70    a
          2   上午    36    50    40         65    a
          3   下午    10    10    20         20    a
    实现结果:
        日期    上午      下午    最高温度   最低湿度   记录人
             温度 湿度  温度 湿度      
         1    10  20    25   60     35        70        a
         2    25  50    null null   36        50        a
         3    null null 10   10     20        20        a
    请大侠赐教。
    结果表里面的最高温度和最低湿度怎么算出来的
      

  3.   

    create table ai (日期 tinyint, 时间 nvarchar(10),   温度 tinyint,  湿度 tinyint,  最高温度 tinyint, 最高湿度 tinyint, 记录人 nvarchar(100))
    insert into ai values( 1,   '上午',    10,    20,    30,         50,    'a')
    insert into ai values(1  , '下午'    ,25   , 60  ,  35      ,   70   , 'a')
    insert into ai values(2   ,'上午' ,   36   , 50  ,  40        , 65    ,'a')
    insert into ai values(3   ,'下午' ,   10 ,   10  ,  20        , 20  ,  'a')
    select 日期,
    avg(case 时间 when '上午' then 温度 else null end) as 上午温度,
    avg(case 时间 when '上午' then 湿度 else null end) as 上午湿度,
    avg(case 时间 when '下午' then 温度 else null end) as 下午温度,
    avg(case 时间 when '下午' then 湿度 else null end) as 下午湿度,
    max(最高温度) as 最高温度,
    min(湿度) as 最低湿度,
    max(记录人) as 记录人
    from ai 
    group by 日期最低湿度是当天湿度里面最低的吗
      

  4.   

    select 日期
          ,(select max(温度) from T where a.日期 = 日期 and 时间 = '上午')
          ,(select min(湿度) from T where a.日期 = 日期 and 时间 = '上午')
          ,(select min(温度) from T where a.日期 = 日期 and 时间 = '下午')
          ,(select max(湿度) from T where a.日期 = 日期 and 时间 = '下午')
          ,(select max(最高温度) from T where a.日期 = 日期 )
          ,(select min(最低湿度) from T where a.日期 = 日期 )
          ,max(记录人) --记录人可能会不太准确,除非同一天上下午是一人
    from T a
    group by 日期
      

  5.   

    select a.currentdate, a.wendu, a.shidu, b.wendu,b.shidu, (case when a.maxwendu <= b.maxwendu then b.maxwendu else a.maxwendu end),
     (case when a.maxshidu <= b.maxshidu then b.maxshidu else a.maxshidu end), a.man
    from 表 a, 表 b
    where a.currentdate = b.currentdate and a.currenttime = '上午' and b.currenttime = '下午'
    union
    select a.currentdate, a.wendu, a.shidu,null,null, a.maxwendu, a.maxshidu, a.man
    from 表 a
    where a.currenttime = '上午' and not exists(select 1 from cg_test_1 b where a.currentdate = b.currentdate and b.currenttime = '下午') 
    union
    select a.currentdate, null,null, a.wendu,a.shidu, a.maxwendu, a.maxshidu ,a.man
    from 表 a
    where a.currenttime = '下午' and not exists(select 1 from cg_test_1 b where a.currentdate = b.currentdate and b.currenttime = '上午')