我有一个包含以下字段的表:
-location
-metername
-measurement
-measuredate
我已经编写了一个 PIVOT 查询,以便metername 列中的每个值现在都是它自己的列。但是,我想限制这一点,使其仅显示基于测量字段的最新仪表读数。有什么建议么?
SELECT LOCATION, MEASUREDATE,
[HEIGHT], [DIAM], [HEIGHT-N],
[HEIGHT-W], [HEIGHT-C], [HEIGHT-E],
[HEIGHT-S], [DIAM-N], [DIAM-W],
[DIAM-C], [DIAM-E], [DIAM-S]
FROM
(
SELECT LOCATION, MEASUREDATE, METERNAME, MEASUREMENTVALUE
FROM MEASUREMENT
) P
PIVOT
(
SUM(MEASUREMENTVALUE)
FOR METERNAME IN ([HEIGHT], [DIAM], [HEIGHT-N], [HEIGHT-W], [HEIGHT-C],
[HEIGHT-E], [HEIGHT-S], [DIAM-N], [DIAM-W], [DIAM-C],
[DIAM-E], [DIAM-S])
) AS PVT
ORDER BY MEASUREDATE DESC