从基于DATE的SQL结果中排除数据

时间:2014-10-22 18:17:38

标签: sql sql-server

我构建了以下查询:我的问题是,如果我在policy_exp_date中获得了今天的日期,我需要排除所有与insplans或clientplans相关的内容,并且仍然返回指定的其他内容。

总结:如果Policy_Exp_DATE>然后TODAY然后拉* else排除insplans。*或clientplans。*

select clients.CLIENT_ID_1 as SourceMRN,
 '' AS HMOMEMBER,
 '' as IDXMRN,
replace (clients.CLIENT_ID_2,'-','') as PatientSSN,
clients.LAST_NAME as LASTNAME,
clients.FIRST_NAME as FIRSTNAME,
clients.MIDDLE_NAME as MIDDLENAME,
 '' as PTMothersMaiden,
convert (varchar,clients.BIRTH_DATE,112) as DOB, 
clients.sex as GENDER,
 '' as PTNICKNAME,
 '' as RACE,
clients.ADDRESS_1,
clients.ADDRESS_2,
clients.CITY,
clients.STATE,
clients.ZIP_CODE,
 '' as FLD18,
 '' as FLD19,
 clients.PHONE_1 as PTHOMEPHONE,
 '' as PTCELL,
 '' as PTEMAIL,
 '' as PTDAYPHONE, 
 (case WHEN [CLIENTS].LANGUAGE_ID LIKE '0' THEN 'ENGLISH' 
 when [clients].LANGUAGE_ID like '1' then 'SPANISH' END) as LANGUAGE,
 '' as MaritalStatus,
 '' as Religion,
 '' as Ethnicity,
 '' as PtExpDate,
 '' as PtExpIndex,
 '' as LASTUPDATEDATE,
 convert (varchar,clientlog.FNDATE,112) as LASTUPDATEDATE,
 '' as PCPNAME,
 '' as VIP,
 '' as FLD33,
 '' as PREFCONTACT,
 '' as FLD35,
 '' as GUARANTORNAME,
 '' AS GUARADDRESS1,
 '' AS GUARADDRESS2,
 '' AS GUARCITY,
 '' AS GUARSTATE,
 '' AS GUARZIP,
 '' AS GUARHOMEPHONE,
 '' AS GUARWORKPHONE,
 '' AS GUARRELATIONSHIP,
 '' as GUARSSN,
 '' as GUAREMPNAME,
 '' as FLD47,
 '' AS GUAREMPADDRESS1,
 '' as GUAREMPADDRESS2,
 '' as GUAREMPCITY,
 '' AS GUAREMPSTATE,
 '' AS GUAREMPZIP,
 '' AS GUAREMPPHONE,
 '' AS GUAREMPSTATUS,
 '' AS FLD55,
 '' AS FLD56,
 '' AS FLD57,
 '' AS FLD58,
 '' AS FLD59,
 '' AS FLD60,
 '' AS FLD61,
 '' AS FLD62,
 '' AS FLD63,
 '' AS FLD64,
 '' AS FLD65,
 insplans.DESCRIPTION_UPPER AS FINANCIAL_CLASS,
 '' as PAYERID#,
 insplans.DESCRIPTION_UPPER as PAYERNAME,
 '' as PAYERADDRESS2,
 '' AS PAYERCITY,
 '' AS PAYERSTATE,
 '' AS PAYERZIP,
 '' AS PAYERCONTACTNBR,
 clientplans.GROUP_NO AS GROUPNUMBER,
 convert (varchar,clientplans.START_DATE,112) as POLICY_EFF_DATE,
 convert (varchar,clientplans.END_DATE,112) as POLICY_EXP_DATE,
 clientplans.SEQUENCE_NUM,
(clients.LAST_NAME +'^' + clients.FIRST_NAME)as INSURED_NAME,
 '' as INSURED_DOB,
 '' AS INSURED_ADD1,
 '' AS INSURED_ADD2,
 '' AS INSURED_CITY,
 '' AS INSURED_STATE,
 '' AS INSURED_ZIP,
 '' AS INSURED_PLAN_NBR,
 clientplans.POLICY_NO AS POLICY_NBR,
 '' AS INSURED_GENDER,
 '' AS INSURED_HOMEPHONE,
 '' AS INSURED_EMP_NAME,
 '' AS INSURED_RELATION,
 '' AS ACCIDENT_DATE,
 '' AS ACCIDENT_CODE,
 '' AS ACCIDENT_STATE,
 '' AS FLD94,
 '' AS CLINICAL_REMINDER,
 '' AS APPT_REMINDER,
 '' AS INS_INST,
 '' AS SET_ID,
 '' AS COPAY,
 'CC' AS SOURCE_PREFIX
 FROM project.dbo.CLIENTS
 join CLIENTPLANS
    on clients.RECORD_ID = clientplans.CLIENT_ID
 join INSPLANS
    on insplans.RECORD_ID = clientplans.PLAN_ID
 join (SELECT MAX(fndate) FNDATE,client_id
 from CLIENTLOG
 group by CLIENT_ID)CLIENTLOG
 on clientlog.CLIENT_ID = clients.RECORD_ID
 where clients.RECORD_ID = clientplans.CLIENT_ID 

1 个答案:

答案 0 :(得分:0)

会不会有用?

FROM project.dbo.CLIENTS
 join (SELECT MAX(fndate) FNDATE,client_id
        from CLIENTLOG
        group by CLIENT_ID)CLIENTLOG
    on clientlog.CLIENT_ID = clients.RECORD_ID
 Left Outer join CLIENTPLANS
    on clients.RECORD_ID = clientplans.CLIENT_ID
      And clientplans.END_DATE > GetDate()
 Left Outer join INSPLANS
    on clientplans.PLAN_ID = insplans.RECORD_ID

还要确保删除where子句,否则您将删除大量记录。