我试图让这个sql逻辑工作,最后一部分是我没有正确的逻辑。
我需要为已列为ID_1& ID_2。如果一个人在30天内有继续阅读,那么即使他们去了另一个地方,他们也会获得readmit_conf_30号码。我需要在报告中提供原始提供者编号和其他提供者,如果他们在30天之内有再接收。
我尝试了很多方法,但对于如何言辞却感到茫然。 Group1适用于我正在寻找的提供商。 Group2适用于其他提供商。 Group3将它们联合在一起,Group4我尝试将它们分配给Y / N,无论我是否想要报告它们。这就是我在案例逻辑中需要帮助的地方。
感谢您的帮助。
代码:
WITH Group1 AS
(
SELECT
A.MEMBER, B.SEX, B.DOB, B.LAST_NAME, B.FIRST_NAME,
A.PROVIDER_ID, C.PROVIDER_NAME, A.CONF_NUM, A.BEG_DT, A.END_DT, A.TOS_I_4, A.DIS_STAT, A.AMT_EQV, A.AMT_PAY, A.AMT_REQ
, A.READMIT_INDEX_30, A.READMIT_30, A.READMIT_CONF_30, '04/30/2014' as DateCheck
, '341425870018' AS ID_1
, '341425870052' AS ID_2
FROM CONFINEMENTS A
LEFT JOIN MEMINFO B ON A.MEMBER = B.MEMBER
LEFT JOIN PROVINFO C ON A.PROVIDER_ID = C.PROVIDER_ID
LEFT JOIN MAP_DATE_RANGE D ON D.IA_TIME = A.IA_TIME
WHERE
A.CUST_OPL_MED IN ('A$*', 'B$*', 'N$*', 'P$*', 'D$*', ' 'Unspecified$UNK')
AND A.BEG_DT >= '05/01/2013' and A.beg_dt <'05/01/2014'
and A.PROVIDER_ID Like '341425870%'
and (A.READMIT_30 = 1 OR A.READMIT_30 = 0)
--AND A.DIS_STAT <> 20
),
GROUP2 AS (
SELECT
A.MEMBER, B.SEX, B.DOB, B.LAST_NAME, B.FIRST_NAME, A.PROVIDER_ID, C.PROVIDER_NAME
, A.CONF_NUM, A.BEG_DT, A.END_DT, A.TOS_I_4, A.DIS_STAT, A.AMT_EQV, A.AMT_PAY, A.AMT_REQ
, A.READMIT_INDEX_30, A.READMIT_30, A.READMIT_CONF_30, '04/30/2014' as DateCheck
, '341425870018' AS ID_1
, '341425870052' AS ID_2
FROM CONFINEMENTS A
LEFT JOIN MEMINFO B ON A.MEMBER = B.MEMBER
LEFT JOIN PROVINFO C ON A.PROVIDER_ID = C.PROVIDER_ID
LEFT JOIN MAP_DATE_RANGE D ON D.IA_TIME = A.IA_TIME
WHERE
A.CUST_OPL_MED IN ('A$*', 'B$*', 'N$*', 'P$*', 'D$*', 'Unspecified$UNK')
AND A.BEG_DT >= '05/01/2013' and A.beg_dt <'05/01/2014'
--and A.PROVIDER_ID Like '341425870%'
and (A.READMIT_30 = 1 OR A.READMIT_30 = 0)
--AND A.DIS_STAT <> 20
),
GROUP3 AS (
Select *, DATEADD(dd, 30, datecheck) AS MaxdateAllowed from Group1
union
select *, DATEADD(dd, 30, datecheck) AS MaxdateAllowed from GROUP2
),
Group4 AS (
select *
, case when PROVIDER_ID <> ID_1 and readmit_conf_30 is not null THEN 'Y'
when PROVIDER_ID <> ID_2 and readmit_conf_30 is not null THEN 'Y'
when PROVIDER_ID = ID_1 and readmit_conf_30 is not null THEN 'Y'
when PROVIDER_ID = ID_2 and readmit_conf_30 is not null THEN 'Y'
when PROVIDER_ID = ID_1 THEN 'Y'
when PROVIDER_ID = ID_2 THEN 'Y'
when PROVIDER_ID <> ID_1 THEN 'N'
when PROVIDER_ID <> ID_2 THEN 'N'
ELSE 'B'
END AS GoodRecord
FROM Group3
where maxdateallowed > beg_dt and READMIT_INDEX_30 = 1
--and (READMIT_30 = 1 OR READMIT_30 = 0)
)
SELECT * FROM Group4
WHERE GoodRecord = 'Y'
order by conf_num
答案 0 :(得分:0)
我明白了。 下面的SQL代码: -
WITH Group1 AS (
SELECT A.MEMBER, B.SEX, B.DOB, B.LAST_NAME, B.FIRST_NAME, A.PROVIDER_ID, C.PROVIDER_NAME
, A.CONF_NUM, A.BEG_DT, A.END_DT, A.TOS_I_4, A.DIS_STAT, A.AMT_EQV, A.AMT_PAY, A.AMT_REQ
, A.READMIT_INDEX_30, A.READMIT_30, A.READMIT_CONF_30
FROM CONFINEMENTS A
LEFT JOIN MEMINFO B ON A.MEMBER = B.MEMBER
LEFT JOIN PROVINFO C ON A.PROVIDER_ID = C.PROVIDER_ID
LEFT JOIN MAP_DATE_RANGE D ON D.IA_TIME = A.IA_TIME
WHERE A.CUST_OPL_MED IN ('A$*', 'B$*', 'N$*', 'P$*', 'D$*', 'Unspecified$UNK')
AND A.BEG_DT >= '05/01/2013' and A.beg_dt <'05/01/2014'
and A.PROVIDER_ID Like '341425870%'
and (A.READMIT_30 = 1 OR A.READMIT_30 = 0)
),
GROUP2 AS (
SELECT AA.MEMBER, AA.READMIT_CONF_30
FROM Group1 AA
WHERE AA.READMIT_CONF_30 > 0
),
Group3 AS (
Select AA.MEMBER, A.READMIT_CONF_30, A.CONF_NUM
,B.SEX, B.DOB, B.LAST_NAME, B.FIRST_NAME, A.PROVIDER_ID, C.PROVIDER_NAME
, A.BEG_DT, A.END_DT, A.TOS_I_4, A.DIS_STAT, A.AMT_EQV, A.AMT_PAY, A.AMT_REQ
, A.READMIT_30, A.READMIT_INDEX_30
FROM GROUP2 AA
LEFT JOIN CONFINEMENTS A ON A.MEMBER = AA.MEMBER AND AA.READMIT_CONF_30 = A.CONF_NUM
LEFT JOIN MEMINFO B ON A.MEMBER = B.MEMBER
LEFT JOIN PROVINFO C ON A.PROVIDER_ID = C.PROVIDER_ID
LEFT JOIN MAP_DATE_RANGE D ON D.IA_TIME = A.IA_TIME
WHERE A.CUST_OPL_MED IN ('A$*', 'B$*', 'N$*', 'P$*', 'D$*', 'Unspecified$UNK')
and (A.READMIT_30 = 1 OR A.READMIT_30 = 0)
)
select
MEMBER, READMIT_CONF_30
,SEX, DOB, LAST_NAME, FIRST_NAME, PROVIDER_ID, PROVIDER_NAME
, CONF_NUM, BEG_DT, END_DT, TOS_I_4, DIS_STAT, AMT_EQV, AMT_PAY, AMT_REQ
, READMIT_30, READMIT_INDEX_30
from group1
union
select
MEMBER, READMIT_CONF_30
,SEX, DOB, LAST_NAME, FIRST_NAME, PROVIDER_ID, PROVIDER_NAME
, CONF_NUM, BEG_DT, END_DT, TOS_I_4, DIS_STAT, AMT_EQV, AMT_PAY, AMT_REQ
, READMIT_30, READMIT_INDEX_30
from Group3
order by conf_num