如何才能使这个 SQL 查询更简单/更短?

发布于 2024-12-10 00:23:38 字数 676 浏览 1 评论 0原文

它应该从 SystemTable 和 ProcessHistory 表中最新记录的 LoadStatus 列返回一些字段。关系是一对多:

SELECT ST.[SystemDetailID], ST.[SystemName], LH.LatestLoadStatus
FROM [SystemTable] AS ST
LEFT OUTER JOIN 
    (
     SELECT LHInner.LoadStatus AS LatestLoadStatus, LHInner.SystemDetailID FROM [dbo].[LoadHistory] AS LHInner
        WHERE LHInner.LoadHistoryID in 
        (
            SELECT LatestLoadHisotoryID FROM 
            (
                SELECT MAX(LoadHistoryID) as LatestLoadHisotoryID, SystemDetailID FROM [dbo].[LoadHistory]
                GROUP BY SystemDetailID
            ) l
        )   
    ) AS LH ON ST.SystemDetailID = LH.SystemDetailID

谢谢,

It should return some fields from the SystemTable and the LoadStatus column of the latest record in the ProcessHistory table. The relationship is 1 to many:

SELECT ST.[SystemDetailID], ST.[SystemName], LH.LatestLoadStatus
FROM [SystemTable] AS ST
LEFT OUTER JOIN 
    (
     SELECT LHInner.LoadStatus AS LatestLoadStatus, LHInner.SystemDetailID FROM [dbo].[LoadHistory] AS LHInner
        WHERE LHInner.LoadHistoryID in 
        (
            SELECT LatestLoadHisotoryID FROM 
            (
                SELECT MAX(LoadHistoryID) as LatestLoadHisotoryID, SystemDetailID FROM [dbo].[LoadHistory]
                GROUP BY SystemDetailID
            ) l
        )   
    ) AS LH ON ST.SystemDetailID = LH.SystemDetailID

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海之角 2024-12-17 00:23:38

这是每组最大 n 查询。

一种方法

SELECT ST.[SystemDetailID],
       ST.[SystemName],
       LH.LatestLoadStatus
FROM   [SystemTable] AS ST
       OUTER APPLY (SELECT TOP 1 *
                    FROM   [dbo].[LoadHistory] LH
                    WHERE  ST.SystemDetailID = LH.SystemDetailID
                    ORDER  BY LoadHistoryID DESC) LH  

您还可以使用 row_number

WITH LH
     AS (SELECT *,
                ROW_NUMBER() OVER (PARTITION BY SystemDetailID 
                                       ORDER BY LoadHistoryID DESC) RN
         FROM   [dbo].[LoadHistory])
SELECT ST.[SystemDetailID],
       ST.[SystemName],
       LH.LatestLoadStatus
FROM   [SystemTable] AS ST
       LEFT JOIN LH
         ON LH.SystemDetailID = ST.SystemDetailID
            AND LH.RN = 1  

This is a greatest-n-per-group query.

One Approach

SELECT ST.[SystemDetailID],
       ST.[SystemName],
       LH.LatestLoadStatus
FROM   [SystemTable] AS ST
       OUTER APPLY (SELECT TOP 1 *
                    FROM   [dbo].[LoadHistory] LH
                    WHERE  ST.SystemDetailID = LH.SystemDetailID
                    ORDER  BY LoadHistoryID DESC) LH  

You can also use row_number

WITH LH
     AS (SELECT *,
                ROW_NUMBER() OVER (PARTITION BY SystemDetailID 
                                       ORDER BY LoadHistoryID DESC) RN
         FROM   [dbo].[LoadHistory])
SELECT ST.[SystemDetailID],
       ST.[SystemName],
       LH.LatestLoadStatus
FROM   [SystemTable] AS ST
       LEFT JOIN LH
         ON LH.SystemDetailID = ST.SystemDetailID
            AND LH.RN = 1  
若相惜即相离 2024-12-17 00:23:38
SELECT ST.[SystemDetailID], ST.[SystemName], LH.LatestLoadStatus
FROM [SystemTable] AS ST
INNER JOIN [dbo].[LoadHistory] AS LH 
ON ST.SystemDetailID = LH.SystemDetailID
AND LH.LoadHistoryID IN 
                     (SELECT MAX(LoadHistoryID) as LoadHistoryID 
                      FROM [dbo].[LoadHistory]
                      GROUP BY SystemDetailID )
SELECT ST.[SystemDetailID], ST.[SystemName], LH.LatestLoadStatus
FROM [SystemTable] AS ST
INNER JOIN [dbo].[LoadHistory] AS LH 
ON ST.SystemDetailID = LH.SystemDetailID
AND LH.LoadHistoryID IN 
                     (SELECT MAX(LoadHistoryID) as LoadHistoryID 
                      FROM [dbo].[LoadHistory]
                      GROUP BY SystemDetailID )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文