2016-11-15 91 views
1

我想选择一组符合特定条件的Ids,并且我努力编写将返回我想要的SQL查询。SQL从一个组的两个表中选择最大ID

表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 

我这两个表试图按网站ID和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 

回答

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 
+0

完整联接返回只用右手记录,然后s2.maxrequestid为NULL - 这样行不INNER JOIN回来并消失 - 如果这是一个问题 - 您可能需要检查 – Cato

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