选择具有最新日期时间值的记录

时间:2013-02-05 08:26:05

标签: sql datetime select sql-server-2008-r2

我有三张桌子

建筑物

BuildingKey(int) | BuildingID(varchar) | ...

领域

areaKey(int) | areaID(varchar) | buildingKey(int-FK[Buildings]) | ...

交易

transactionKey(int) | areaKey(int-FK[Areas]) | value(float) | trnDateTime(DateTime)


Areas中可能有多个Building

所有区域都有许多Transactions,不同valuetrnDateTime不同。

我想要做的是获取value的每个 Area的最新Building(交易)(当buildingKeyDECLARE @buildingKey INT SET @buildingKey = 3 ;WITH Vals AS ( SELECT T.areaKey AS AreaKey, T.value AS CurrentValue, T.trnDateTime AS RecordedDateTime, ROW_NUMBER() OVER (PARTITION BY B.buildingKey ORDER BY T.trnDateTime DESC) RowID FROM Buildings B INNER JOIN Areas A ON B.buildingKey = A.buildingKey INNER JOIN Transactions T ON A.areaKey = T.areaKey WHERE B.buildingKey = @buildingKey ) SELECT AreaKey, CurrentValue, MAX(RecordedDateTime) AS RecentReading, RowID FROM Vals WHERE RowID = 1 GROUP BY AreaKey, CurrentValue, RowID 时给出)。

我之前提到了this one之类的一些问题,并尝试了以下问题。

(1)

DECLARE @buildingKey INT
SET @buildingKey = 3

SELECT  A.areaKey AS AreaKey, 
        A.areaID AS AreaID,
        T.value AS CurrentValue,
        T.trnDateTime AS RecordedDateTime                   

FROM Areas A, Buildings B, Transactions T

WHERE   @buildingKey = B.buildingKey AND 
        B.buildingKey = A.buildingKey AND
        T.areaKey = A.areaKey AND
        T.trnDateTime IN (SELECT MAX(T.trnDateTime), T.areaKey
                                  FROM Transactions T
                                  GROUP BY T.areaKey)

!)返回最新值(在所有区域中); 每个区域的最新值!

(2)

Msg 116, Level 16, State 1, Line 16
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

!)给出错误 - >

{{1}}

3 个答案:

答案 0 :(得分:2)

这将为您提供每个建筑物的最新区域。

(PARTITION BY B.BuildingKey, a.areaKey ORDER BY T.TransactionDateTime DESC) RowID

答案 1 :(得分:2)

SELECT  A.areaKey AS AreaKey, 
        A.areaID AS AreaID,
        T.value AS CurrentValue,
        T.trnDateTime AS RecordedDateTime                   
FROM Areas A JOIN Buildings B ON B.buildingKey = A.buildingKey 
             JOIN Transactions T ON T.areaKey = A.areaKey 
WHERE @buildingKey = B.buildingKey AND EXISTS (
                                       SELECT 1
                                       FROM Transactions T2
                                       WHERE T.areaKey = T2.areaKey
                                       GROUP BY T2.areaKey
                                       HAVING MAX(T2.trnDateTime) = T.trnDateTime
                                               ) 

答案 2 :(得分:1)

尝试

DECLARE @buildingKey INT
SET @buildingKey = 3

SELECT  A.areaKey AS AreaKey, 
        A.areaID AS AreaID,
        T.value AS CurrentValue,
        T.trnDateTime AS RecordedDateTime

FROM Areas A, Buildings B, Transactions T

WHERE   @buildingKey = B.buildingKey AND 
        B.buildingKey = A.buildingKey AND
        T.areaKey = A.areaKey AND
        T.trnDateTime IN (SELECT MAX(T.trnDateTime)
                                  FROM Transactions T
                                  GROUP BY T.areaKey)