2017-08-10 28 views
1

SQL Gurus、特定の制約を受けて行を互いに比較するコードのヘルプを探しています。以下は私のテーブルで見ているもののほんの一部です。私ができることをしたいのは、不十分な行のReview_Dateよりも大きいFax Dateの確認済みのReview_Detail_Statusがある行のみを返すことです。注:同じProcessing_Instanceを持つバッチを比較します。同じテーブルの行を日付と比較するにはどうすればよいですか? (SQL Server)

Processing_Instance Review_Id    GMPI     MemberID    Review_Date    FAX_DATE REVIEW_DETAIL_STATUS 
    ------------------- ---------------------- ---------------------- ---------------------- ----------------------- ----------------------- ------------------- 
    23760    11359973    650775278    300601690600   2017-03-30 00:00:00.000 2017-03-27 00:00:00.000 Insufficient 
    23760    11237889    650775278    300601690600   2017-03-01 00:00:00.000 2017-02-28 00:00:00.000 Insufficient 
    23760    11359973    650775278    300601690600   2017-03-30 00:00:00.000 2017-03-27 00:00:00.000 Confirmed 
    23760    11359973    650775278    300601690600   2017-03-30 00:00:00.000 2017-03-27 00:00:00.000 Confirmed 
    23760    11237889    650775278    300601690600   2017-03-01 00:00:00.000 2017-02-28 00:00:00.000 Insufficient 

今、私はこのコードを持っています。

SELECT Processing_Instance, 
     Review_Id, 
     GMPI, 
     MemberID, 
     Review_Date, 
     ATTESTATION_FAX_DATE, 
     REVIEW_DETAIL_STATUS 
FROM TEST 
WHERE EXISTS 
(
    SELECT 1 
    FROM TEST AS WT2 
    WHERE WT2.Processing_Instance = TEST.Processing_Instance 
    and WT2.GMPI=Test.GMPI 
    and WT2.FAX_DATE>TEST.REVIEW_DATE 
      /*AND WT2.GMPI = 650775278*/ 
and WT2.Processing_Instance=23760 
); 

しかし、それが返されます。

Processing_Instance Review_Id    GMPI     MemberID    Review_Date    FAX_DATE REVIEW_DETAIL_STATUS 
------------------- ---------------------- ---------------------- ---------------------- ----------------------- ----------------------- ----------------------- 
23760    11237889    650775278    300601690600   2017-03-01 00:00:00.000 2017-02-28 00:00:00.000 Insufficient 
23760    11237889    650775278    300601690600   2017-03-01 00:00:00.000 2017-02-28 00:00:00.000 Insufficient 

私は(理論的に)取得する必要があります。

Processing_Instance Review_Id    GMPI     MemberID    Review_Date    FAX_DATE REVIEW_DETAIL_STATUS 
    ------------------- ---------------------- ---------------------- ---------------------- ----------------------- ----------------------- ----------------------- 
     23760    11359973    650775278    300601690600   2017-03-30 00:00:00.000 2017-03-27 00:00:00.000 Confirmed 
     23760    11359973    650775278    300601690600   2017-03-30 00:00:00.000 2017-03-27 00:00:00.000 Confirmed 

感謝を!

+0

は、なぜあなたはこれを持っているとき、あなたはその2番目の結果セットを取得すると思いますか?あなたが実際に得ている2つの行は、この基準を満たす唯一の2つです。 – SqlZim

答えて

0

日付比較を反転し、前のインスタンスのステータスが'Insufficient'であり、返される行が'Confirmed'であることが必要です。

select 
    Processing_Instance 
    , Review_Id 
    , gmpi 
    , Memberid 
    , Review_Date 
    , attestation_fax_date 
    , review_detail_status 
from test 
where review_detail_status = 'confirmed' 
    and exists (
    select 1 
    from test as wt2 
    where wt2.Processing_Instance = test.Processing_Instance 
     and wt2.gmpi=Test.gmpi 
     and wt2.review_detail_status = 'Insufficient' 
     and wt2.attestation_fax_date<test.review_date 
     and wt2.Processing_Instance=23760  
); 

rextesterデモ:http://rextester.com/htysu28824

リターン:

+---------------------+-----------+-----------+--------------+---------------------+----------------------+----------------------+ 
| Processing_Instance | Review_Id | gmpi | Memberid |  Review_Date  | attestation_fax_date | review_detail_status | 
+---------------------+-----------+-----------+--------------+---------------------+----------------------+----------------------+ 
|    23760 | 11359973 | 650775278 | 300601690600 | 2017-03-30 00:00:00 | 2017-03-27 00:00:00 | Confirmed   | 
|    23760 | 11359973 | 650775278 | 300601690600 | 2017-03-30 00:00:00 | 2017-03-27 00:00:00 | Confirmed   | 
+---------------------+-----------+-----------+--------------+---------------------+----------------------+----------------------+ 
0

サブクエリとしてあなたのリターンの結果を使用してください。こうすることで、必要なものを手に入れ、必要なテーブル/サブクエリにリンクすることができます。ただし、サブクエリの一意のIDの1つを使用する必要があるので、リンクがあることを覚えておいてください。 IE:Processing_ID、Review ID、またはMemberIDが機能するはずです。

あなたの他のテーブルを知らなくても、私はあなたが他のテーブル

Select a.column1, a.column2, a.Processing_Instance_ID, b.* 

--Whatever table you are linking your subquery below to 
from table a 

--This join should only return the 2 rows where the Review Date > Fax Date 
--This is your subquery 
JOIN (Select Processing_Instance, 
      Review_Id, 
      GMPI, 
      MemberID, 
      Review_Date, 
      ATTESTATION_FAX_DATE, 
      REVIEW_DETAIL_STATUS 
    From test 
    Where Review_Date > Attestation_Fax_Date 
     /***Note: You can also do WHERE Review_detail_status = 'Confirmed' 
     rather than the date comparison. Regardless, you get the same results 
     **/ 


    ) b on a.Processing_Instance_ID 
     = b.Processing_Instance 
0

にProcessing_Instance IDをリンクすることができますと仮定します結果はあなたのようではないですが、あなたが探している本ですか?あなた `存在する()` `WHERE`句で` WT2.ATTESTATION_FAX_DATE> TEST.REVIEW_DATE`:

http://sqlfiddle.com/#!6/af37d/10

SELECT distinct t1.* 
FROM TEST t1 
JOIN TEST t2 
ON 
     t2.Processing_Instance = t1.Processing_Instance /* comparing batches that have the same Processing_Instance. */ 
WHERE 
     t1.REVIEW_DETAIL_STATUS = 'Confirmed'   /*have a Review_Detail_Status of Confirmed*/ 
     and t1.ATTESTATION_FAX_DATE > t2.Review_Date /*WITH a Fax Date that is greater than the Insufficient row's (see the below) Review_Date*/ 
     and t2.REVIEW_DETAIL_STATUS = 'Insufficient' /* the Insufficient row */ 
関連する問題