我有两张桌子 1.资格 2. DebitCardTransaction
我想将状态列更新为' Y'在debitcardtransaction表中,如果服务日期落在任何BenefitEffectiveDate& GracePeriodEndDate在资格表中其他' N'
这两个表之间有两个共同的字段。一个是SSN&第二是EIN。
例如服务日期是2016-03-02在debitcardtransaction表中SSN = 139668966和EIN = 137580
现在我需要查看此服务日期是否介于BenefitEffectiveDate&来自资格表的GracePeriodEndDate,SSN = 139668966,EIN = 137580
对于此示例,它不会介于BenefitEffectiveDate&的任何日期范围之间。 GracePeriodEndDate为SSN = 139668966,EIN = 137580
有人可以帮我解决这个问题吗?
以下是资格表结构
+----------------------+--------------------+-----------+--------+
| BenefitEffectiveDate | GracePeriodEndDate | SSN | EIN |
+----------------------+--------------------+-----------+--------+
| 2016-01-01 | 2016-01-31 | 139668966 | 137580 |
| 2016-03-01 | 2016-03-30 | 139668966 | 137580 |
| 2016-04-06 | 2016-05-30 | 139668966 | 137580 |
| 2016-01-01 | 2017-02-02 | 539980084 | 137580 |
| 2016-01-01 | 2016-06-01 | 570728149 | 137580 |
| 2016-08-01 | 2016-10-02 | 570728149 | 137580 |
| 2016-01-01 | 2017-02-02 | 624621535 | 137580 |
| 2016-01-01 | 2017-02-02 | 651442905 | 137580 |
| 2016-01-01 | 2017-02-02 | 651442905 | 137580 |
+----------------------+--------------------+-----------+--------+
以下是DebitcardTransaction表
+-------------+--------+-----------+--------+
| ServiceDate | EIN | SSN | Status |
+-------------+--------+-----------+--------+
| 2016-01-20 | 137580 | 139668966 | |
| 2016-02-01 | 137580 | 139668966 | |
| 2016-03-02 | 137580 | 139668966 | |
| 2016-06-02 | 137580 | 139668966 | |
| 2016-02-02 | 137580 | 539980084 | |
| 2016-02-02 | 137580 | 570728149 | |
| 2016-11-02 | 137580 | 570728149 | |
| 2016-01-01 | 137580 | 624621535 | |
| 2016-02-01 | 137580 | 651442905 | |
+-------------+--------+-----------+--------+
我正在寻找输出
+-------------+--------+-----------+--------+
| ServiceDate | EIN | SSN | Status |
+-------------+--------+-----------+--------+
| 2016-01-20 | 137580 | 139668966 | Y |
| 2016-02-01 | 137580 | 139668966 | N |
| 2016-03-02 | 137580 | 139668966 | Y |
| 2016-06-02 | 137580 | 139668966 | N |
| 2016-02-02 | 137580 | 539980084 | Y |
| 2016-02-02 | 137580 | 570728149 | Y |
| 2016-11-02 | 137580 | 570728149 | N |
| 2016-01-01 | 137580 | 624621535 | Y |
| 2016-02-01 | 137580 | 651442905 | Y |
+-------------+--------+-----------+--------+
答案 0 :(得分:0)
试试这个,
UPDATE DT
SET DT.Status = (CASE WHEN EXISTS(
SELECT 1
FROM eligibility E
WHERE E.SSN = DT.SSN AND E.EIN = DT.EIN
AND DT.ServiceDate BETWEEN E.BenefitEffectiveDate AND GracePeriodEndDate
) THEN 'Y' ELSE 'N' END)
FROM DebitcardTransaction DT
答案 1 :(得分:0)
这是一个带有可更新CTE的工作示例:
DECLARE @tbl1 TABLE (BenefitEffectiveDate DATE,GracePeriodEndDate DATE,SSN INT,EIN INT);
INSERT INTO @tbl1 VALUES
('2016-01-01','2016-01-31',139668966,137580 )
,('2016-03-01','2016-03-30',139668966,137580 )
,('2016-04-06','2016-05-30',139668966,137580 )
,('2016-01-01','2017-02-02',539980084,137580 )
,('2016-01-01','2016-06-01',570728149,137580 )
,('2016-08-01','2016-10-02',570728149,137580 )
,('2016-01-01','2017-02-02',624621535,137580 )
,('2016-01-01','2017-02-02',651442905,137580 )
,('2016-01-01','2017-02-02',651442905,137580 );
DECLARE @tbl2 TABLE(ServiceDate DATE,EIN INT,SSN INT,Status CHAR(1));
INSERT INTO @tbl2 VALUES
('2016-01-20',137580,139668966,NULL)
,('2016-02-01',137580,139668966,NULL)
,('2016-03-02',137580,139668966,NULL)
,('2016-06-02',137580,139668966,NULL)
,('2016-02-02',137580,539980084,NULL)
,('2016-02-02',137580,570728149,NULL)
,('2016-11-02',137580,570728149,NULL)
,('2016-01-01',137580,624621535,NULL)
,('2016-02-01',137580,651442905,NULL);
WITH myCTE AS
(
SELECT *
,CASE WHEN EXISTS(SELECT 1
FROM @tbl1 AS t1
WHERE t1.EIN=t2.EIN
AND t1.SSN=t2.SSN
AND t2.ServiceDate>=t1.BenefitEffectiveDate
AND t2.ServiceDate< CAST(t1.GracePeriodEndDate AS DATETIME)+1)
THEN 'Y' ELSE 'N' END AS NewStatus
FROM @tbl2 AS t2
)
UPDATE myCTE SET Status = NewStatus;
SELECT * FROM @tbl2;
答案 2 :(得分:0)
我在一个select语句中尝试了它并包含了连接的所有条件。 请查看以下内容:
SELECT
T2.ServiceDate
,T2.SSN
,T2.EIN
,[Status] = CASE WHEN T1.BenefitEffectiveDate IS NULL THEN 'N' ELSE 'Y' END
FROM DebitcardTransaction T2
LEFT JOIN Eligibility T1
ON T1.SSN = T2.SSN
AND T1.EIN = T2.EIN
AND T2.ServiceDate BETWEEN T1.BenefitEffectiveDate AND T1.GracePeriodEndDate
GROUP BY
T2.ServiceDate
,T2.SSN
,T2.EIN
,T1.BenefitEffectiveDate
ORDER BY 2
如果这只是一个不好的数据,那么您不需要该组:
| 2016-01-01 | 2017-02-02 | 651442905 | 137580 |
| 2016-01-01 | 2017-02-02 | 651442905 | 137580 |