我有一个sql查询(使用MS-SQL 2005),它使用以下列生成数据:
TimeStamp | SpeedMax | SpeedMin | HeightMax | HeightMin
-------------------------------------------------------
10 | 50 | 10 | 300 | 70
我需要它的形式是:
TimeStamp | Speed | Height
---------------------------
10 | 50 | 300 <-- one row for the max values
10 | 10 | 70 <-- a second row for the min values
鉴于第一个结果集...我需要什么查询才能将数据转换为第二种格式?我认为它可能涉及到一个不透明的东西,但我是新手,并且我在编写要写的东西时遇到了麻烦。
非常感谢。
答案 0 :(得分:2)
SELECT TimeStamp,
minmax,
CASE WHEN minmax = 0 THEN SpeedMax ELSE SpeedMin END AS Speed
CASE WHEN minmax = 0 THEN HeightMax ELSE HeightMin END AS Height
FROM Table,
(
SELECT 0 AS minmax
UNION ALL
SELECT 1
) mm
您可以添加
OPTION (FORCE ORDER)
避免Table
答案 1 :(得分:1)
尝试
SELECT TimeStamp, SpeedMax AS Speed, HeightMax AS Height
FROM Table
UNION ALL
SELECT TimeStamp, SpeedMin AS Speed, HeightMin AS Height
FROM Table