2016-04-15 8 views
0

このSQLロジックを動作させようとしていますが、最後の部分はロジックが正しくありません。Cognos Report Studio 10.2.2 case statement sql logic

ID_1 & ID_2と記載されているプロバイダの再読みを取り戻す必要があります。ある人が30日以内に再送信された場合、別の場所に行った場合でもreadmit_conf_30番号を取得します。お互いに30日以内に再送信がある場合は、元のプロバイダー番号と他のプロバイダーをレポートに入れる必要があります。

私は多くの方法を試してきました。 Group1は、私が探しているプロバイダ向けです。グループ2は他のプロバイダ向けです。グループ3の組合を一緒にして、グループ4は私が彼らを報告したいかどうかを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

私はそれを考え出しました。 以下の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