简体   繁体   中英

If condition is met then do the following

I'm struggling with creating a query and was hoping to get some guidance from those wiser than me. I'm trying to write a query such that when DRG Code = 005, then take all of the HOSP_ADMT_DT from this query and then query the general claims table again without the DRG = 5 condition and pull all claims that are 30 days or before the admit date and 180 days or less after the admit date and then join the results.

Edit: The result I'm looking for would run the query below. I need this result. And then take all of the Member ID's and and Admit dates resulting from the query below and execute a query that Pulls all of the original fields without the DRG criteria = 005, but criteria on member id's that had DRG 5 and admit date and then pull all claims from this subset and the pull all claims in range Admit Date (defined by:HOSP_ADMT_DT) - 30 and Admit Date + 180. CLM_LN_SVC_FROM_DT ,CLM_LN_SVC_TO_DT - Defined by

SELECT CLM_MED_DETL_ID
       ,CLM_ID_TXT
       ,CLM_SRC_CD
       ,CLM_LN_SEQ_NUM
       ,MBR_ID
       ,MBR_LST_NAME
       ,MBR_FST_NAME
       ,SVCG_PROV_ADDR_ZIP_CD
       ,SVCG_PROV - ADDR_CNTY
       ,SBMTD_DRG_CD
       ,HOSP_ADMT_DT
       ,HOSP_DSCHRG_DT
       ,RVNU_CD
       ,CLM_LN_UNITS_NUM
       ,CLM_LN_CHRG_AMT
       ,CLM_LN_ALWD_AMT
       ,CLM_LN_PD_AMT
       ,TIN_TXT
       ,HOSP_ADMT_DT
       ,CLM_LN_SVC_FROM_DT
       ,CLM_LN_SVC_TO_DT
    FROM CLM_MED_DETL
    WHERE TIN_TXT IN ( 'A', 'B' )
        AND SBMTD_DRG_CD = '005'
        AND CLM_SRC_CD = 'TRG_FA'
    ORDER BY MBR_LST_NAME ASC
       ,CLM_LN_SEQ_NUM ASC;

Try something like this

... WHERE (SBMTD_DRG_CD='005' OR (SBMTD_DRG_CD <>'005 AND (claim is > 30 days OR laim.date < (180 + admit.date)) ))

I don't know how your dates are stored. (Sorry,writing on phone,can't format code)

I think you need smth like this:

  • the CD_005 subquery returns the medical details for DRG criteria = 005, I've also included your additional conditions there, you may want to amend them
  • And the CD_ANY alias contains all the records from that table

When we join these aliases on MBR_ID and HOSP_ADMT_DT range, we receive all the records within [-30;+180] dates range around '005' claim for the clients who ever had '005' claim.

WITH CD_005 as (
    SELECT  MBR_ID, HOSP_ADMT_DT
        FROM CLM_MED_DETL
        WHERE TIN_TXT IN ( 'A', 'B' )
            AND SBMTD_DRG_CD = '005'
            AND CLM_SRC_CD = 'TRG_FA'
)
SELECT CLM_MED_DETL_ID
       ,CLM_ID_TXT
       ,CLM_SRC_CD
       ,CLM_LN_SEQ_NUM
       ,CD_ANY.MBR_ID
       ,MBR_LST_NAME
       ,MBR_FST_NAME
       ,SVCG_PROV_ADDR_ZIP_CD
       ,SVCG_PROV - ADDR_CNTY
       ,SBMTD_DRG_CD
       ,CD_ANY.HOSP_ADMT_DT
       ,HOSP_DSCHRG_DT
       ,RVNU_CD
       ,CLM_LN_UNITS_NUM
       ,CLM_LN_CHRG_AMT
       ,CLM_LN_ALWD_AMT
       ,CLM_LN_PD_AMT
       ,TIN_TXT
       ,HOSP_ADMT_DT
       ,CLM_LN_SVC_FROM_DT
       ,CLM_LN_SVC_TO_DT
   FROM CLM_MED_DETL CD_ANY 
   JOIN CD_005 
     on CD_ANY.MBR_ID = CD_005.MBR_ID 
    and CD_ANY.HOSP_ADMT_DT >= CD_005.HOSP_ADMT_DT -  30
    and CD_ANY.HOSP_ADMT_DT <= CD_005.HOSP_ADMT_DT + 180
  ORDER BY MBR_LST_NAME ASC
       ,CLM_LN_SEQ_NUM ASC;

Hope it runs well and really helps, I just can't check it as I don't have such tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM