我有一些数据:
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来自哪个列...
答案 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
如果您不使用CASE
语句,col
字段只会显示值starttime
或endtime
。
结果:
| 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 |