【发布时间】:2012-11-12 17:16:03
【问题描述】:
我需要交叉表或数据透视表通过选择日期时间。
表格文件TA
EmpNo ChkDate ChkIn
00001 2012-10-10 00:00:00.000 2012-10-10 07:22:00.000
00002 2012-10-10 00:00:00.000 2012-10-10 07:30:00.000
00001 2012-10-11 00:00:00.000 2012-10-11 07:13:00.000
00002 2012-10-11 00:00:00.000 2012-10-11 07:34:00.000
00001 2012-10-12 00:00:00.000 2012-10-12 07:54:00.000
00002 2012-10-12 00:00:00.000 2012-10-12 07:18:00.000
我已尝试关注
SELECT tf.EmpNo,tf.ChkDate,tf.ChkIn
FROM (SELECT EmpNo,ChkDate,ChkIn
,ROW_NUMBER() OVER(PARTITION BY EmpNo ORDER BY ChkDate) as tfNum
FROM filesTA) AS tf
PIVOT(MIN(ChkDate) FOR tfNum IN ('2012-10-10'))
WHERE tf.ChkDate Between '2012-10-10' and '2012-10-12'
但出现以下错误
Incorrect syntax near 'PIVOT'. You may need to set the compatibility
level of the current database to a higher value to enable this feature.
See help for the SET COMPATIBILITY_LEVEL option of ALTER DATABASE.
所需的输出:
EmpNo 10 11 12
00001 07:22 07:13 07:54
00002 07:30 07:34 07:18
我开始学习数据透视表和交叉表。请帮助我让我的查询正常工作。
【问题讨论】:
-
你运行的是什么版本的sql-server?兼容级别错误表明您需要将数据库兼容级别更新到至少 9 (2005)...
-
我正在运行 SQL SERVER 2008 R2
-
您尚未接受/评论 bluefeet 的回答。它正在提供您想要的输出。你还需要什么吗?
标签: sql sql-server datetime pivot crosstab