select car_value=max(case when pid='car' then value else -1 end),
       book_value=max(case when pid='book' then value else -1 end),  
       cup_value=max(case when pid='cup' then value else -1 end),  
       updatetime from table_info group by updatetime

解决方案 »

  1.   

    select 
       car_value=sum(case when pid='car' then  value else 0 end),
       book_value=sum(case when pid='book' then  value else 0 end),
      

  2.   

    select 
       car_value=sum(case when pid='car' then  value else 0 end),
       book_value=sum(case when pid='book' then  value else 0 end),
       cup_value=sum(case when pid='cup' then  value else 0 end),
       updatetime
    from table_info 
    group by updatetime
      

  3.   

    select 
      car_value=sum(car_value),book_value=sum(book_value),cup_value=sum(cup_value),updatetime=max(updatetime)
    from
      (select 
        car_value=(case pid when 'car' then value else 0 end),
        book_value=(case pid when 'book' then value else 0 end),
        cup_value=(case pid when 'cup' then value else 0 end),
        updatetime=(case pid when 'updatetime' then updatetime else 0 end)
      from table_info) info
    group by updatetime
      

  4.   

    谢谢楼上的各位~~  
    现在出现了新的问题,updatetime 这项的时间是精确到毫秒的
    2005-12-19 10:00:00.195
    2005-12-19 10:00:00.652
    这样的形式,用group by updatetime这样的得出的结果肯定是不行的,因为时间实际是有毫秒级的区别,怎样去掉毫秒的信息,让这两个时间相等?
      

  5.   

    select car_value=max(case when pid='car' then value else -1 end),
           book_value=max(case when pid='book' then value else -1 end),  
           cup_value=max(case when pid='cup' then value else -1 end),  
           updatetime=Convert(varchar(30),updatetime,120)
     from table_info
     group by Convert(varchar(30),updatetime,120)
      

  6.   

    因为有毫秒的差别,结果就变成下面的结果了
    0.0 0.0 2005-12-01 01:02:00.000
    0.0 0.18     2005-12-01 01:02:30.500
    0.74 0.0 2005-12-01 01:02:30.813
    0.0 0.0 2005-12-01 01:03:00.000
    0.0 0.18 2005-12-01 01:03:30.500
    0.74 0.0 2005-12-01 01:03:30.843
    0.0 0.0 2005-12-01 01:04:00.000
    0.0 0.18 2005-12-01 01:04:30.483
    0.74 0.0 2005-12-01 01:04:30.797
    0.0 0.0 2005-12-01 01:05:00.000
    用的是 churchatp1(别看资料,看聊效!) 的方法
    实际上上面的数据中是没有值为0的,现在因为这个毫秒,出来很多个0~~~
      

  7.   

    上面的结果已经用 WangZWang(阿来)的方法去掉了毫秒信息,为什么用group by 就会在秒钟为0的时候加一条记录阿?实际上当秒钟为0的时候,不一定会有数据存在的~~~但是用了group by 就硬是把数据给加上去了?
      

  8.   

    select car_value=max(case when pid='car' then value else -1 end),
           book_value=max(case when pid='book' then value else -1 end),  
           cup_value=max(case when pid='cup' then value else -1 end),  
           updatetime=Convert(varchar(19),updatetime,120)
     from table_info
     group by Convert(varchar(19),updatetime,120)