结果重复 - 无法浓缩

时间:2016-03-17 18:16:45

标签: sql-server group-by distinct

此报告的结果列出了同一客户7-9次。我不完全确定为什么。尝试过GROUPING和DISTINCT。 DISTINCT将结果细化为每个客户端仅3个重复项。

也许我忽略了一些显而易见的事情。

简而言之,我正在

ClientID | First_Name |姓氏

123 .................约翰........................约翰

123 .................约翰........................约翰

123 .................约翰........................约翰

456 .................鲍勃..........................鲍勃< / p>

456 .................鲍勃..........................鲍勃< / p>

456 .................鲍勃..........................鲍勃< / p>

我需要的是:

ClientID | First_Name |姓氏

123 .................约翰........................约翰

456 .................鲍勃..........................鲍勃< / p>

但是GROUP BY或DISTINCT没有解决问题。

SELECT  
     c.clientID
    ,c.firstName AS 'First_Name'
    ,c.lastName  AS 'Last_Name'
    ,c.homePhone AS 'Home'
    ,c.cellPhone AS 'Cell'
    ,NULL        AS '------'
    ,CASE
        WHEN ccf.name LIKE 'First Contact Date' THEN ccv.TextVal
     END         AS 'First_Contact_Date'
    ,CASE
        WHEN ccf.name LIKE 'Referral Date' THEN ccv.DateVal
     END         AS 'Referral_Date'
    ,CASE
        WHEN ccf.name LIKE 'Referred By' THEN ccv.TextVal
     END         AS 'Referred_By'
    ,CASE
        WHEN ccf.name LIKE 'Intake Date' THEN ccv.DateVal
     END         AS 'Intake_Date'
    ,CASE
        WHEN ccf.name LIKE 'Intake By' THEN ccv.TextVal
     END         AS 'Intake_By'
    ,CASE
        WHEN ccf.name LIKE 'Orientation Date' THEN ccv.TextVal
     END         AS 'Orientation_Date'
    ,CASE
        WHEN civ.clientIndexID = 5 THEN civ.clientIndexValueName
     END         AS 'Primary_Language'
    ,NULL        AS '------'
    ,CASE
        WHEN civ.clientIndexID = 18 THEN civ.clientIndexValueName
     END         AS 'Type'
    ,CASE
        WHEN civ.clientIndexID = 20 THEN civ.clientIndexValueName
     END         AS 'Stage_at_intro'
    ,CASE
        WHEN civ.clientIndexID = 21 THEN civ.clientIndexValueName
     END         AS 'Current_stage'
    ,NULL        AS '------'
    ,CASE
        WHEN civ.clientIndexID = 19 THEN civ.clientIndexValueName
     END         AS 'Percentage'
    ,CASE
        WHEN civ.clientIndexID = 26 THEN civ.clientIndexValueName
     END         AS 'Location'
    ,CASE
        WHEN civ.clientIndexID = 27 THEN civ.clientIndexValueName
     END         AS 'Eligible?'

FROM clients c
    INNER JOIN tblClientCustomValues ccv
        ON c.clientID = ccv.clientID
    INNER JOIN tblClientCustomFields ccf
        ON ccv.ID = ccf.ID
    INNER JOIN tblClientIndexData cid
        ON c.clientID = cid.clientID
    INNER JOIN tblClientIndexValue civ
        ON cid.clientIndexValueID = civ.clientIndexValueID
GROUP BY c.clientID
        ,c.firstName
        ,c.lastName
        ,c.homePhone
        ,c.cellPhone
        ,ccf.name
        ,ccv.TextVal
        ,ccv.DateVal
        ,civ.clientIndexValueName
        ,civ.clientIndexID

有关表格的更多信息:

tblClientCustomFields ccf:

ID |名称| TYPEID

tblClientIndexData cid:

ClientIndexValueID |客户端ID

tblClientIndexValue civ:

ClientIndexValueID | ClientIndexID | ClientIndexValueName

tblClientCustomValues ccv:

ID | ClientID | TextVal | DateVal | NumVal

1 个答案:

答案 0 :(得分:1)

您可以从逐个列表中删除信息列,并将max()添加到您的代码中,或将所有聚合放入子查询中:

SELECT  
     c.clientID
    ,c.firstName AS 'First_Name'
    ,c.lastName  AS 'Last_Name'
    ,c.homePhone AS 'Home'
    ,c.cellPhone AS 'Cell'
    ,NULL        AS '------'
    ,info.*
FROM clients c
OUTER APPLY
(
  SELECT
    max(CASE
        WHEN ccf.name LIKE 'First Contact Date' THEN ccv.TextVal
     END)         AS 'First_Contact_Date'
    ,max(CASE
        WHEN ccf.name LIKE 'Referral Date' THEN ccv.DateVal
     END)         AS 'Referral_Date'
    ,max(CASE
        WHEN ccf.name LIKE 'Referred By' THEN ccv.TextVal
     END)         AS 'Referred_By'
    ,max(CASE
        WHEN ccf.name LIKE 'Intake Date' THEN ccv.DateVal
     END)         AS 'Intake_Date'
    ,max(CASE
        WHEN ccf.name LIKE 'Intake By' THEN ccv.TextVal
     END)         AS 'Intake_By'
    ,max(CASE
        WHEN ccf.name LIKE 'Orientation Date' THEN ccv.TextVal
     END)         AS 'Orientation_Date'
    ,max(CASE
        WHEN civ.clientIndexID = 5 THEN civ.clientIndexValueName
     END)         AS 'Primary_Language'
    ,NULL        AS '------'
    ,max(CASE
        WHEN civ.clientIndexID = 18 THEN civ.clientIndexValueName
     END)         AS 'Type'
    ,max(CASE
        WHEN civ.clientIndexID = 20 THEN civ.clientIndexValueName
     END)         AS 'Stage_at_intro'
    ,max(CASE
        WHEN civ.clientIndexID = 21 THEN civ.clientIndexValueName
     END)         AS 'Current_stage'
    ,max(CASE
        WHEN civ.clientIndexID = 19 THEN civ.clientIndexValueName
     END)         AS 'Percentage'
    ,max(CASE
        WHEN civ.clientIndexID = 26 THEN civ.clientIndexValueName
     END)         AS 'Location'
    ,max(CASE
        WHEN civ.clientIndexID = 27 THEN civ.clientIndexValueName
     END)         AS 'Eligible?'
    FROM tblClientCustomValues ccv
    INNER JOIN tblClientCustomFields ccf
        ON ccv.ID = ccf.ID
    INNER JOIN tblClientIndexData cid
        ON ccv.clientID = cid.clientID
    INNER JOIN tblClientIndexValue civ
        ON cid.clientIndexValueID = civ.clientIndexValueID
    WHERE c.clientID = ccv.clientID
) info