SQL Gurus,我正在寻找一些代码帮助,以便在某些约束条件下将行相互比较。以下是我在桌子上看到的一小部分内容。我想要做的是只返回行的帽子有一个已确认的Review_Detail_Status,其传真日期大于Insufficient行的Review_Date。注意:我将比较具有相同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
现在,我有这个代码。
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 :(得分:0)
翻转您的日期比较,并要求先前的实例状态为String value = "0000000013";
printWriter.println("=\"" + value + "\"");
,并且返回的行为'Insufficient'
。
'Confirmed'
rextester演示:http://rextester.com/htysu28824
返回:
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
);
答案 1 :(得分: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
答案 2 :(得分: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 */