0

我想使用 SQL Server 2008 R2 查询 UNPIVOT Cnvert Column to Rows。

但这是错误

**Msg 170, Level 15, State 1, Line 16
Line 16: Incorrect syntax near 'unpivot'.**

代码:

SELECT EmpNo, CDate, CTime
FROM
(SELECT EmpNo,ChkDate,ChkIn,ChkOut
  FROM TA_FillTime) ta1
UNPIVOT
(CTime FOR CDate IN (ChkIn, ChkOut)
) AS ta2

表 TA_FillTime:

EmpNo   | ChkDate       | ChkIn                   | ChkOut  
1290005 | 2013-05-09    | 2013-05-09 07:04:26.000 | 2013-05-09 17:57:36.000 
1320005 | 2013-05-09    | 2013-05-09 07:56:17.000 | 2013-05-09 18:24:35.000 
1320007 | 2013-05-09    | 2013-05-09 07:45:50.000 | 2013-05-09 17:24:55.000 

我需要输出:

EmpNo   | CDate      | CTime    
1290005 | 2013-05-09 | 2013-05-09 07:04:26.000
1290005 | 2013-05-09 | 2013-05-09 17:57:36.000
1320005 | 2013-05-09 | 2013-05-09 07:56:17.000
1320005 | 2013-05-09 | 2013-05-09 18:24:35.000
1320007 | 2013-05-09 | 2013-05-09 07:45:50.000
1320007 | 2013-05-09 | 2013-05-09 17:24:55.000

谢谢你的时间,:)

4

1 回答 1

0
select 
EmpNo, ChkDate, CDate = ChkIn
from TA_FillTime
union all
select 
EmpNo, ChkDate, CDate = ChkOut
from TA_FillTime
order by 
EmpNo, ChkDate
于 2013-05-09T10:22:36.787 回答