来自 SQL Group By Query 的添加列

Addition Columns from SQL Group By Query

我正在尝试聚合通过 4 table 和 return 最后一个链接行链接的数据,基于每个包含的房间的第一个 table 中的时间戳条目在最后 table.

当我使用以下查询时,我根据需要对条目进行了优先排序和分组:

SELECT 
    RoomName,
    CRV_SignalLog.SignalID,
    Max(LogTimeStamp) As LogTime
FROM 
    [CRV_SignalLog]
    inner Join [CRV_SymbolSignals]
        on CRV_SignalLog.SignalID = CRV_SymbolSignals.SignalID
    inner Join [CRV_Symbols]
        on CRV_SymbolSignals.SymbolID = CRV_Symbols.SymbolID
    inner Join [CRV_Rooms]
        on CRV_Symbols.RoomID = CRV_Rooms.RoomID
Where 
    AttributeID = 'Display_Power'
Group By 
    RoomName, 
    CRV_SignalLog.SignalID
Order By 
    LogTime Desc 

我得到了所有房间的列表以及每个房间的最后一个 LogTime 条目。但是我需要从 CRV_SignalLog table 添加另一个字段以了解该信号的当前状态。

我一直在研究并根据其他答案,我认为我可以将这些结果与 CRV_SignalLog table 结合起来,从 table 添加其他列,如下所示:

SELECT
    RoomName,
    DigitalValue,
    LastEntry.LogTime
From
    (SELECT 
        RoomName,
        CRV_SignalLog.SignalID,
        Max(LogTimeStamp) as LogTime
    FROM 
        [CRV_SignalLog]
        inner Join [CRV_SymbolSignals]
            on CRV_SignalLog.SignalID = CRV_SymbolSignals.SignalID
        inner Join [CRV_Symbols]
            on CRV_SymbolSignals.SymbolID = CRV_Symbols.SymbolID
        inner Join [CRV_Rooms]
            on CRV_Symbols.RoomID = CRV_Rooms.RoomID
    where 
        AttributeID = 'Display_Power'
    Group By 
        RoomName, 
        CRV_SignalLog.SignalID
    ) LastEntry
    Inner Join CRV_SignalLog 
        on CRV_SignalLog.SignalID = LastEntry.SignalID 
Order By 
    RoomName

但是,不幸的是,这会破坏结果 - 我认为 return 为每个匹配的 'AttributeID = 'Display_Power' 设置一行,但显示每个房间最后一个条目的时间戳。

我错过了什么?

发生的情况是,您在 CRV_SignalLog 中的每一行都获得了与 SignalID 字段的子查询结果相匹配的结果。换句话说,在子查询中应用 Group By 后,它正在 LastEntry 和 CRV_SignalLog 之间进行连接。

我会尝试在 CRV_SignalLog.LogTimeStamp 和 LastEntry.LogTime 字段之间添加另一个连接条件,如下所示:

SELECT
    RoomName,
    DigitalValue,
    LastEntry.LogTime
From
    (SELECT 
        RoomName,
        CRV_SignalLog.SignalID,
        Max(LogTimeStamp) as LogTime
    FROM 
        [CRV_SignalLog]
        inner Join [CRV_SymbolSignals]
            on CRV_SignalLog.SignalID = CRV_SymbolSignals.SignalID
        inner Join [CRV_Symbols]
            on CRV_SymbolSignals.SymbolID = CRV_Symbols.SymbolID
        inner Join [CRV_Rooms]
            on CRV_Symbols.RoomID = CRV_Rooms.RoomID
    where 
        AttributeID = 'Display_Power'
    Group By 
        RoomName, 
        CRV_SignalLog.SignalID
    ) LastEntry
    Inner Join CRV_SignalLog 
        on CRV_SignalLog.SignalID = LastEntry.SignalID 
        AND CRV_SignalLog.LogTimeStamp = LastEntry.LogTime
Order By 
    RoomName

我假设 LogTimeStamp 在 CRV_SignalLog table 上。

由于您担心LogTimeStamp是否足够唯一,这里再举一个使用子查询获取DigitalValue的例子,保证只获取一个:

SELECT
    RoomName,
    (SELECT TOP 1 
        DigitalValue
     FROM
        CRV_SignalLog dvLog
     WHERE
        dvLog.SignalID = LastEntry.SignalID
        AND dvLog.LogTimeStamp = LastEntry.LogTime
    ) DigitalValue,
    LastEntry.LogTime
From
    (SELECT 
        RoomName,
        CRV_SignalLog.SignalID,
        Max(LogTimeStamp) as LogTime
    FROM 
        [CRV_SignalLog]
        inner Join [CRV_SymbolSignals]
            on CRV_SignalLog.SignalID = CRV_SymbolSignals.SignalID
        inner Join [CRV_Symbols]
            on CRV_SymbolSignals.SymbolID = CRV_Symbols.SymbolID
        inner Join [CRV_Rooms]
            on CRV_Symbols.RoomID = CRV_Rooms.RoomID
    where 
        AttributeID = 'Display_Power'
    Group By 
        RoomName, 
        CRV_SignalLog.SignalID
    ) LastEntry
Order By 
    RoomName