简体   繁体   中英

How to compare rows of the same table by dates? (SQL Server)

SQL Gurus, I'm looking for some help with code to compare rows to one another WITH certain constraints. Below is a small portion of what I'm looking at in my table. What I want to be able to do is return only the rows hat have a Review_Detail_Status of Confirmed WITH a Fax Date that is greater than the Insufficient row's Review_Date. Note: I will be comparing batches that have the same Processing_Instance.

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

Right now, I have this code.

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
);

But it returns:

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

I should (theoretically) be getting:

 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

Thanks!

Flipping your date comparison, and requiring that the prior instance has a status of 'Insufficient' and the returned rows are '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 demo: http://rextester.com/htysu28824

returns:

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

Use your return result as a subquery. This way you get what you want and link it back to whatever table/subqueries you want. But remember, you must use one of the unique ID from the subquery so there's a link. IE: Processing_ID, Review ID, or MemberID should work.

Without knowing your other tables, I'll assume you can link the Processing_Instance ID to other tables

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

Is this you are looking for, though the result is not as yours?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM