2017-08-10 28 views
1

SQL大师,我正在寻找一些帮助代码来比较行与其他有一定的限制。以下是我在桌上看到的一小部分。我希望能够做的是只返回具有大于Insufficient行的Review_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

为什么你认为当你有这个,你会拿到第二个结果集:你的存在'()''where'子句中'WT2.ATTESTATION_FAX_DATE> TEST.REVIEW_DATE'?这两行你实际得到的是唯一符合这个标准的两个行。 – 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之一,以便有链接。 IE:Processing_ID,Review ID或MemberID应该可以工作。

不知道你的其他表,我会认为你可以链接Processing_Instance ID与其他表

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

这是你正在寻找,但结果不如你?

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 */