日期:2014-05-17  浏览次数:20414 次

Sql预计根据编号查询相同时间的值
有下列表数据
dataId                 tm                 v
21          2012/12/1 00:00:00            13
21          2012/12/1 01:00:00            12
22          2012/12/1 00:00:00            3
22          2012/12/1 01:00:00            1
23          2012/12/1 00:00:00            5
23          2012/12/1 01:00:00            6
24          2012/12/1 00:00:00            7
24          2012/12/1 01:00:00            8
写一条sql语句查询如下结果该怎么写?

 tm                    v1       v2       v3     v4
2012/12/1 00:00:00     13       3        5      7
2012/12/1 01:00:00     12       1        6      8

sql

------解决方案--------------------
竖转横

select tm,
max(case when dataid=21 then v else null end) as v1,
max(case when dataid=22 then v else null end) as v2,
max(case when dataid=23 then v else null end) as v3,
max(case when dataid=24 then v else null end) as v4
from 表名
group by tm