select no,time1=max(case when date='2005-12-01' then time end),
time2=max(case when date='2005-12-02' then time end),
time3=max(case when date='2005-12-03' then time end)
from tablename group by no

解决方案 »

  1.   

    多谢 lxzm1001(*蓝星之梦*)!
    实际的天数是不一定的,所以列数也是不一定的,动态语句该怎么写!多谢!
      

  2.   

    Delcare
         @sCondition  varchar(8000)Select @sCondition = 'select no, 'Select @sCondition = sCondition + 'Max(Case when Date =' + ''' + Date + '''+ 'then Time end ) as '''+ Time +''',' from TableNameselect @sCondition = left(@sCondition ,len(@sCondition )-1) + ' from TableName 'exec(@sCondition )大概是这个意思,没有测试! Sorry
    select no,time1
    =max(case when date='2005-12-01' then time end),
    time2=max(case when date='2005-12-02' then time end),
    time3=max(case when date='2005-12-03' then time end)
    from tablename group by no