SQL将数据收集到列

时间:2014-05-14 11:35:08

标签: sql-server pivot

我有以下查询返回汽车注册号以及查看日期和位置:

SELECT PLATE_READ.RegNumber, PLATE_READ.DateSeen, ANPR_CAMERA.Name FROM PLATE_READ

REG         DATESEEN               LOCATION
===         ========               ========
5897HHS     20/12/2013 12:10:57    EXIT
5897HHS     20/12/2013 12:05:03    ROAD2
5897HHS     20/12/2013 12:01:17    ROAD1

我希望能够查询并且每个注册号返回1行,而不是有三行,而是按照以下格式说明每个位置的日期:

REG         DATESEEN_EXIT        DATESEEN_ROAD2         DATESEEN_ROAD1
===         =============        ==============         ==============
5897HHS     20/12/2013 12:10:57  20/12/2013 12:05:03    20/12/2013 12:01:17

这是我到目前为止所做的:

SELECT  *
FROM (
select TOP(100) PLATE_READ.DateSeen, PLATE_READ.RegNumber, ANPR_CAMERA_GROUP.Name
from  ANPR_CAMERA INNER JOIN
                     ANPR_CAMERA_GROUP ON ANPR_CAMERA.Id = ANPR_CAMERA_GROUP.Id INNER     JOIN
                     ANPR_CAMERA_IN_GROUP ON ANPR_CAMERA.Id = ANPR_CAMERA_IN_GROUP.ANPRCameraId AND 
                     ANPR_CAMERA_GROUP.Id = ANPR_CAMERA_IN_GROUP.ANPRCameraGroupId INNER JOIN
                     PLATE_READ ON ANPR_CAMERA.Id = PLATE_READ.CameraId
ORDER BY PLATE_READ.DateSeen DESC
) x
pivot (
max(DateSeen)
for Name in ([GIB EXIT], [AIRPORT BARRIER])
)p

然而,这总是为' GIB EXIT'返回一个NULL值。条目

0 个答案:

没有答案