如何按日期比较同一表格的行?(SQL Server)
SQL专家,我正在寻找一些代码方面的帮助,以比较具有特定约束的行。下面是我在表格中看到的内容的一小部分。我希望能够做的是只返回状态为“已确认且传真日期大于不足行的审阅日期”的行。注意:我将比较具有相同处理实例的批如何按日期比较同一表格的行?(SQL Server),sql,sql-server,Sql,Sql Server,SQL专家,我正在寻找一些代码方面的帮助,以比较具有特定约束的行。下面是我在表格中看到的内容的一小部分。我希望能够做的是只返回状态为“已确认且传真日期大于不足行的审阅日期”的行。注意:我将比较具有相同处理实例的批 Processing_Instance Review_Id GMPI MemberID Review_Date FAX_DATE REVIEW_DETAIL_S
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
谢谢 翻转日期比较,并要求上一个实例的状态为
“不足”
,返回的行为“已确认”
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
);
翻转日期比较,并要求上一个实例的状态为
“不足”
,返回的行为“已确认”
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
);
将返回结果用作子查询。通过这种方式,您可以获得所需内容,并将其链接回所需的任何表/子查询。但是请记住,您必须使用子查询中的一个唯一ID,以便有一个链接。IE:Processing_ID、Review ID或MemberID应该可以工作 在不知道其他表的情况下,我假设您可以将处理实例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
将返回结果用作子查询。通过这种方式,您可以获得所需内容,并将其链接回所需的任何表/子查询。但是请记住,您必须使用子查询中的一个唯一ID,以便有一个链接。IE:Processing_ID、Review ID或MemberID应该可以工作 在不知道其他表的情况下,我假设您可以将处理实例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
这是你正在寻找的,尽管结果不是你的吗
这是你正在寻找的,尽管结果不是你的吗
为什么你认为你会得到第二个结果集,当你有这样的:
WT2.认证\u传真\u日期>测试。在你的exists()
where
子句中查看日期?您实际获得的这两行是唯一符合此标准的两行。当您有以下内容时,您为什么认为您会获得第二个结果集:WT2.认证\u传真\u日期>测试。在exists()
where
子句中查看日期?您实际得到的这两行是唯一满足此条件的两行。