2016-11-15 2 views
1

特定の条件に一致するIDのセットを選択しようとしていますが、私が欲しいものを返すSQLクエリを書くのに苦労しています。グループの2つのテーブルから最大のidsを選択する

表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を取得し、その後、両方のテーブルの上のsiteIdとCOUNTRYCODEによってグループにしようとしています。次に、サイト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

は、これは私が最後に思い付いた答えが、それは私がそれをしたいどのように動作しますが、あなたはそれを改善する方法を持っている場合、私はしたいと思いますあなたの提案を聞く。 FULL OUTERが使用中JOINを反映しようとする

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

-

 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 
関連する問題