CASE语句返回重复的行。我只想要每行1行

时间:2014-07-29 21:50:06

标签: sql-server-2012 case duplicate-removal

以下CASE语句返回2行,如果此人是成员,则将joindate = 1连接到连接日期(cd.user_d1),如果记录中没有连接日期,则返回null。如何让CASE语句只返回1行,其中joindate字段为null或成员的加入日期?

我搜索过并找到了包含SUM的其他类型字段的解决方案,但没有找到日期字段的解决方案。任何帮助将不胜感激。

SELECT DISTINCT
        c.master_customer_id ,
        c.SUB_CUSTOMER_ID ,
        c.PRIMARY_EMAIL_ADDRESS ,
        c.FIRST_NAME ,
        c.INFORMAL_SALUTATION ,
        c.LAST_NAME ,
        c.LABEL_NAME ,
        c.PRIMARY_JOB_TITLE ,
        cad.company_name ,
        ca.ADDRESS_1 ,
        ca.ADDRESS_2 ,
        ca.ADDRESS_3 ,
        ca.ADDRESS_4 ,
        ca.CITY ,
        ca.STATE ,
        ca.POSTAL_CODE ,
        ca.COUNTRY_DESCR ,
        c.PRIMARY_URL ,
        c.BIRTH_DATE ,
        CASE WHEN cd.DEMOGRAPHIC_SUBCODE = 'join_date'
                  AND cd.user_d1 IS NOT NULL THEN cd.USER_D1
             ELSE NULL
        END AS JOINDATE ,
        CASE WHEN cc.comm_type_code = 'EMAIL'
                  AND cc.primary_flag <> 'Y' THEN cc.formatted_phone_address
        END AS secondary_email
FROM    customer c
        LEFT OUTER JOIN CUS_DEMOGRAPHIC cd ON cd.MASTER_CUSTOMER_ID = c.MASTER_CUSTOMER_ID
        LEFT OUTER JOIN CUS_ADDRESS ca ON ca.MASTER_CUSTOMER_ID = c.MASTER_CUSTOMER_ID
        LEFT OUTER JOIN CUS_ADDRESS_DETAIL cad ON cad.CUS_ADDRESS_ID = ca.CUS_ADDRESS_ID
        LEFT OUTER JOIN CUS_COMMUNICATION cc ON cc.MASTER_CUSTOMER_ID = c.MASTER_CUSTOMER_ID
WHERE   c.CUSTOMER_STATUS_CODE = 'active'
        AND c.ALLOW_EMAIL_FLAG = 'Y'
        AND cad.PRIORITY_SEQ = 0
        AND c.PRIMARY_EMAIL_ADDRESS <> ' '
ORDER BY c.MASTER_CUSTOMER_ID

0 个答案:

没有答案