求一SQL,想了许久都没解决!
表字段及记录如下:
ID T0 ReadTime
000000000001 479.12 2007-2-26 00:15:43
000000000001 479.12 2007-2-26 1:15:43
000000000001 479.12 2007-2-26 2:15:43
000000000001 479.12 2007-2-26 3:15:43
000000000001 479.12 2007-2-26 4:15:43
000000000001 479.12 2007-2-26 5:15:43
000000000001 479.12 2007-2-26 6:15:43
000000000001 479.12 2007-2-26 7:15:43
000000000001 479.12 2007-2-26 8:15:43
000000000001 479.12 2007-2-26 9:15:43
000000000001 479.12 2007-2-26 10:15:43
000000000001 479.12 2007-2-26 11:15:43
000000000001 479.12 2007-2-26 12:15:43
000000000001 479.12 2007-2-26 13:15:43
000000000001 479.12 2007-2-26 14:15:43
000000000001 479.12 2007-2-26 15:15:43
000000000001 479.12 2007-2-26 16:15:43
000000000001 479.12 2007-2-26 17:15:43
000000000001 479.12 2007-2-26 18:15:43
000000000001 479.12 2007-2-26 19:15:43
000000000001 479.12 2007-2-26 20:15:43
000000000001 479.12 2007-2-26 21:15:43
000000000001 479.12 2007-2-26 22:15:43
000000000001 479.12 2007-2-26 23:15:43
现在要将每天的数据分成24点来显示,比如2007-2-26 的数据(见上图),共有24条记录,把这24条记录取出来在一行分24个来显示,对应时间下面是这个时间点的值,如下:
0:00 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00 9:00 10:00 11:00 12:00 13:00 14:00 15:00 16:00 17:00 18:00 19:00 20:00 21:00 22:00 23:00
479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12 479.12
------解决方案--------------------select id,
max(case when right(convert(varchar(13),readtime,120),2) = '01 ' then T0 else 0 end) '0:00 ',
max(case when right(convert(varchar(13),readtime,120),2) = '02 ' then T0 else 0 end) '1:00 ',
max(case when right(convert(varchar(13),readtime,120),2) = '03 ' then T0 else 0 end) '2:00 ',
max(case when right(convert(varchar(13),readtime,120),2) = '04 ' then T0 else 0 end) '3:00 ',
max(case when right(convert(varchar(13),readtime,120),2) = '05 ' then T0 else 0 end) '4:00 ',
max(case when right(convert(varchar(13),readtime,120),2) = '06 ' then T0 else 0 end) '5:00 ',
max(case when right(convert(varchar(13),readtime,120),2) = '07 ' then T0 else 0 end) '6:00 ',
max(case when right(convert(varchar(13),readtime,120),2) = '08 ' then T0 else 0 end) '7:00 ',
max(case when right(convert(varchar(13),readtime,120),2) = '09 ' then T0 else 0 end) '8:00 ',
max(case when right(convert(varchar(13),readtime,120),2) = '01 ' then T0 else 0 end) '9:00 ', <