SQL从一个组的两个表中选择max ids

时间:2016-11-15 11:29:51

标签: sql sql-server select group-by max

我正在尝试选择一组符合特定条件的ID,我正在努力编写将返回我想要的SQL查询。

表1:

SelloutPriceID|SiteID|CountryCode|CurrencyCode|RequestID|RequestDateTime|InsertedDateTime
666|1002|BE|EUR|12504|2016-09-02 11:57:12.0000000|2016-11-14 14:27:35.980
667|1002|BE|EUR|12501|2016-09-02 11:57:12.0000000|2016-11-14 14:27:36.600
668|1002|BE|EUR|12507|2016-09-02 11:57:12.0000000|2016-11-14 14:27:36.963

表2:

SelloutPricesAuditID|RequestID|SiteID|CountryCode|InsertedDateTime
1|128|1002|BE|2016-11-14 16:55:29.543
2|12507|1002|BE|2016-11-14 17:07:16.633

我正在尝试按两个表上的siteId和countryCode进行分组,然后只获取该组的最大请求ID。然后将这些表连接在一起,匹配站点ID和国家/地区代码以及最大请求ID。

如果左表行不在右表中,我希望它返回。 如果右表的请求id不等于左表的maxRequestID,我希望该行返回。

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

SELECT s.*, spa2.*
FROM [SPS_selloutprices].[SelloutPrices] as s WITH (NOLOCK)

inner join (select sp.SiteID, sp.CountryCode, Max(sp.RequestID) as maxrequestid 
            from SPS_selloutprices.SelloutPrices sp
            group by sp.SiteID, sp.CountryCode
            ) s2
            on s2.SiteID = s.SiteID and s.CountryCode = s2.CountryCode and s2.maxrequestid = s.RequestID

full join (select spa.SiteID, spa.CountryCode, MAX(spa.RequestID) as maxrequestid
           from sps_pricealerts.SelloutPricesAudit spa
           group by spa.SiteID, spa.CountryCode
           ) spa2
           on s.SiteID = spa2.SiteID and s.CountryCode = spa2.CountryCode and s2.maxrequestid = spa2.maxrequestid

2 个答案:

答案 0 :(得分:0)

这就是我最终提出的答案,它是我想要的,如果你有办法改进它,我想听听你的建议。

SELECT s.*, spa2.*
FROM [SPS_selloutprices].[SelloutPrices] as s WITH (NOLOCK)

inner join (select sp.SiteID, sp.CountryCode, Max(cast(sp.RequestID as int)) as maxrequestid 
            from SPS_selloutprices.SelloutPrices sp
            group by sp.SiteID, sp.CountryCode
            ) s2
            on s2.SiteID = s.SiteID and s.CountryCode = s2.CountryCode and s2.maxrequestid = s.RequestID

full join (select spa.SiteID, spa.CountryCode, spa.IsRetrieved, max(cast(spa.RequestID as int)) as maxrequestid
           from sps_pricealerts.SelloutPricesAudit spa
           group by spa.SiteID, spa.CountryCode, spa.IsRetrieved
           ) spa2
           on s.SiteID = spa2.SiteID and s.CountryCode = spa2.CountryCode and s2.maxrequestid = spa2.maxrequestid
where ((s.RequestID <> spa2.maxrequestid or spa2.maxrequestid is null)
or (spa2.IsRetrieved <> 1 or spa2.IsRetrieved is null))
and s.CountryCode = @countryCode

答案 1 :(得分:0)

尝试反映正在使用的FULL OUTER JOIN - 正在考虑

        SELECT s.*, spa2.*
    FROM [SPS_selloutprices].[SelloutPrices] as s 

    inner join (select sp.SiteID, sp.CountryCode, Max(sp.RequestID) as maxrequestid 
                from SPS_selloutprices.SelloutPrices sp
                group by sp.SiteID, sp.CountryCode
                ) s2
                on  s2.SiteID = s.SiteID and s.CountryCode = s2.CountryCode and s2.maxrequestid = s.RequestID
                  OR spa2.SiteID = s.SiteID and s.CountryCode = spa2.CountryCode and spa2.maxrequestid = s.RequestID

    full join (select spa.SiteID, spa.CountryCode, MAX(spa.RequestID) as maxrequestid
               from sps_pricealerts.SelloutPricesAudit spa
               group by spa.SiteID, spa.CountryCode
               ) spa2
               on s.SiteID = spa2.SiteID and s.CountryCode = spa2.CountryCode and s2.maxrequestid = spa2.maxrequestid