unpivot()以及其他信息

时间:2012-11-14 10:46:12

标签: sql sql-server tsql unpivot

我有一些数据:

t   starttime                 endtime

1   2012-11-13 07:00:00.000   2012-11-13 09:00:00.000
1   2012-11-13 09:00:00.000   2012-11-13 09:15:00.000
2   2012-11-13 09:00:00.000   2012-11-13 16:00:00.000
1   2012-11-13 09:15:00.000   2012-11-13 12:00:00.000
1   2012-11-13 12:30:00.000   2012-11-13 15:00:00.000
1   2012-11-14 07:00:00.000   2012-11-14 09:00:00.000
1   2012-11-14 09:00:00.000   2012-11-14 09:15:00.000
1   2012-11-14 09:15:00.000   2012-11-14 12:00:00.000
1   2012-11-14 12:30:00.000   2012-11-14 15:00:00.000
1   2012-11-15 07:00:00.000   2012-11-15 09:00:00.000
1   2012-11-15 09:00:00.000   2012-11-15 09:15:00.000
1   2012-11-15 09:15:00.000   2012-11-15 12:00:00.000
1   2012-11-15 12:30:00.000   2012-11-15 15:00:00.000

并希望像以下一样获得预备:

timestamp               t   x

2012-11-13 07:00:00.000 1   1
2012-11-13 09:00:00.000 1   0
2012-11-13 09:00:00.000 1   1
2012-11-13 09:00:00.000 2   1
2012-11-13 09:15:00.000 1   0
2012-11-13 09:15:00.000 1   1
2012-11-13 12:00:00.000 1   0
2012-11-13 12:30:00.000 1   1
2012-11-13 15:00:00.000 1   0
2012-11-13 16:00:00.000 2   0
2012-11-14 07:00:00.000 1   1
2012-11-14 09:00:00.000 1   0
2012-11-14 09:00:00.000 1   1
2012-11-14 09:15:00.000 1   0
2012-11-14 09:15:00.000 1   1
2012-11-14 12:00:00.000 1   0
2012-11-14 12:30:00.000 1   1
2012-11-14 15:00:00.000 1   0
2012-11-15 07:00:00.000 1   1
2012-11-15 09:00:00.000 1   0
2012-11-15 09:00:00.000 1   1
2012-11-15 09:15:00.000 1   0
2012-11-15 09:15:00.000 1   1
2012-11-15 12:00:00.000 1   0
2012-11-15 12:30:00.000 1   1
2012-11-15 15:00:00.000 1   0
如果x的值来自timestamp(x = 1)或来自starttime(x = 0),则

endtime应定义。 我知道,这是一块蛋糕,使用两个selects和一个union all

但我想知道是否可以使用unpivot() 进行一些扩展? 开始像:

SELECT 
    , UD."timeStamp"
    , UD."t"
    , "isStart"
FROM 
    (
        SELECT 
              "StartTime"
            , "EndTime"
            , "t"
            , < REFERENCE TO STARTTIME OR ENDTIME ? >
        FROM 
            Data 
    ) AS BC
UNPIVOT ( "timeStamp" FOR dummy IN ( "StartTime", "EndTime" )) AS UD

< REFERENCE TO STARTTIME OR ENDTIME ? >应该提供信息,timeStamp来自哪个列...

1 个答案:

答案 0 :(得分:2)

这样的事情,只需对它应用CASE语句:

select timestamp,
  t,
  case when col = 'starttime' then 1 else 0 end x
from yourtable
unpivot
(
  timestamp for col in (starttime, endtime)
) un

请参阅SQL Fiddle with Demo

如果您不使用CASE语句,col字段只会显示值starttimeendtime

结果:

|                       TIMESTAMP | T | X |
-------------------------------------------
| November, 13 2012 07:00:00+0000 | 1 | 1 |
| November, 13 2012 09:00:00+0000 | 1 | 0 |
| November, 13 2012 09:00:00+0000 | 1 | 1 |
| November, 13 2012 09:15:00+0000 | 1 | 0 |
| November, 13 2012 09:00:00+0000 | 2 | 1 |
| November, 13 2012 16:00:00+0000 | 2 | 0 |
| November, 13 2012 09:15:00+0000 | 1 | 1 |
| November, 13 2012 12:00:00+0000 | 1 | 0 |
| November, 13 2012 12:30:00+0000 | 1 | 1 |
| November, 13 2012 15:00:00+0000 | 1 | 0 |
| November, 14 2012 07:00:00+0000 | 1 | 1 |
| November, 14 2012 09:00:00+0000 | 1 | 0 |
| November, 14 2012 09:00:00+0000 | 1 | 1 |
| November, 14 2012 09:15:00+0000 | 1 | 0 |
| November, 14 2012 09:15:00+0000 | 1 | 1 |
| November, 14 2012 12:00:00+0000 | 1 | 0 |
| November, 14 2012 12:30:00+0000 | 1 | 1 |
| November, 14 2012 15:00:00+0000 | 1 | 0 |
| November, 15 2012 07:00:00+0000 | 1 | 1 |
| November, 15 2012 09:00:00+0000 | 1 | 0 |
| November, 15 2012 09:00:00+0000 | 1 | 1 |
| November, 15 2012 09:15:00+0000 | 1 | 0 |
| November, 15 2012 09:15:00+0000 | 1 | 1 |
| November, 15 2012 12:00:00+0000 | 1 | 0 |
| November, 15 2012 12:30:00+0000 | 1 | 1 |
| November, 15 2012 15:00:00+0000 | 1 | 0 |