-2

出于某种原因,我的查询返回重复行。我正在尝试创建一个SELECT DISTINCT查询。有人可以告诉我我的查询有什么问题吗?

SELECT DISTINCT
    PT_AGCY_DTL.MLM_AGCY_NBR AS AgencyID
    , AGENCY.ORG_NM AS Agency
    , AGCY_ADDR.CTY_NM as AgencyCity
    , AGCY_ADDR.ST_PRVN_CDE_CID as AgencyST
    , AG_AGCY_CNTCT.AGCY_CNTCT_ID as ContactID
    , AG_AGCY_CNTCT.CNTCT_DT AS ContactDt
    , AG_AGCY_CNTCT.AGCY_RESULTS_TXT AS AgencyResults
    , AG_AGCY_CNTCT.GEN_OVERVIEW_TXT AS GeneralOverview
    , AG_AGCY_CNTCT.NB_RNWL_BUS_DISCUSSION_TXT AS NewAndRenewalBusinessDiscussions
    , AG_AGCY_CNTCT.NB_RNWL_BUS_DISCUSSION_TXT AS NewAndRenewalBusinessDiscussions
    , AG_AGCY_CNTCT.MKT_INTELLIGENCE_TXT AS MarketIntelligence
    , AG_AGCY_CNTCT.AGCY_PERSONNEL_CHG_TXT AS AgencyPersonnelChanges
    , AG_AGCY_CNTCT.MM_ISSUE_TXT AS MyMonitorIssues
    , AG_AGCY_CNTCT.UW_CLM_ISSUE_TXT AS UnderwritingClaimIssues
    , AG_AGCY_CNTCT.FOLLOW_UP_ITEM_TXT AS FollowUpActionItems
    , AG_AGCY_CNTCT.NXT_CNTCT_DT AS NextScheduledVisitDate
    , CONTACT_TYPE.CODE_NM AS ContactType
    , CONVERT(VARCHAR, AG_AGCY_CNTCT.CNTCT_DT,101) + ' - ' + CONTACT_TYPE.CODE_NM AS ContactDtType
    , AG_AGCY_CNTCT.CNTCT_DESC AS ContactDetails
, CASE
     WHEN PRODUCER.LST_NM IS NULL THEN ' '
     ELSE LTRIM(RTRIM(PRODUCER.FRST_NM)) + ' ' + LTRIM(RTRIM(PRODUCER.LST_NM)) END AS Producers
, CASE
     WHEN MLM_EMPL.LST_NM IS NULL THEN ' '
     ELSE LTRIM(RTRIM(MLM_EMPL.FRST_NM)) + ' ' + LTRIM(RTRIM(MLM_EMPL.LST_NM)) END AS Employees
, CASE 
     WHEN PROD_CAT.CODE_NM IS NULL THEN ' '
     ELSE LTRIM(RTRIM(PROD_CAT.CODE_NM)) END AS ProductCategory

FROM
    AG_AGCY_CNTCT
    INNER JOIN PT_AGCY_DTL
        ON AG_AGCY_CNTCT.AGCY_PID = PT_AGCY_DTL.PARTY_ID
    INNER JOIN PT_PARTY AS AGENCY
        ON AGENCY.PARTY_ID = AG_AGCY_CNTCT.AGCY_PID
    LEFT OUTER JOIN PT_PARTY_ADDR AS AGCY_ADDR
        ON AGCY_ADDR.PARTY_ID = AG_AGCY_CNTCT.AGCY_PID
    INNER JOIN CD_CODE AS CONTACT_TYPE
        ON CONTACT_TYPE.CODE_ID = AG_AGCY_CNTCT.CNTCT_TYP_CID
    LEFT OUTER JOIN AG_AGCY_CNTCT_PRDCR_RLTNSHP
        ON AG_AGCY_CNTCT_PRDCR_RLTNSHP.AGCY_CNTCT_ID = AG_AGCY_CNTCT.AGCY_CNTCT_ID
            LEFT OUTER JOIN PT_PARTY AS PRODUCER
        ON PRODUCER.PARTY_ID = AG_AGCY_CNTCT_PRDCR_RLTNSHP.PRDCR_PID
     LEFT OUTER JOIN AG_AGCY_CNTCT_MLM_EMPL_RLTNSHP
        ON AG_AGCY_CNTCT_MLM_EMPL_RLTNSHP.AGCY_CNTCT_ID = AG_AGCY_CNTCT.AGCY_CNTCT_ID
    LEFT OUTER JOIN PT_PARTY AS MLM_EMPL
        ON MLM_EMPL.PARTY_ID = AG_AGCY_CNTCT_MLM_EMPL_RLTNSHP.MLM_EMPL_PID
    LEFT OUTER JOIN AG_AGCY_CNTCT_PROD_CAT_TYP_RLTNSHP
        ON AG_AGCY_CNTCT_PROD_CAT_TYP_RLTNSHP.AGCY_CNTCT_ID = AG_AGCY_CNTCT.AGCY_CNTCT_ID
    LEFT OUTER JOIN CD_CODE AS PROD_CAT
        ON PROD_CAT.CODE_ID = AG_AGCY_CNTCT_PROD_CAT_TYP_RLTNSHP.PROD_CAT_TYP_CID
        AND AGCY_ADDR.ADDR_TYP_CID = '30'  -- business address
        AND AGCY_ADDR.REC_STS_TYP_CID = 'A'  -- active
WHERE
    PT_AGCY_DTL.MLM_AGCY_NBR ='4759' --@AgencyID (this is the FILTER)

ORDER BY ContactDt DESC, ContactID DESC
4

1 回答 1

0

您没有指定,但我怀疑您的印象是以下行是重复的:

t1.col1   | t1.col2   | t2.col1
--------------------------------
1         | 1         | 1
1         | 1         | 2

不是这种情况。正如评论中提到的,DISTINCT适用于您选择的所有列。您可以看到有两种不同的值组合,因此将返回两行。有些人会使用聚合函数和 group by 来解决这个问题。例子:

SELECT t1.col1, t1.col2, MAX(t2.col1)
FROM table1 t1
INNER JOIN table2 t2
on t1.whatever = t2.whatever
GROUP BY t1.col1, t1.col2

这只会返回第二行。如果您这样做,请确保您了解您排除信息是为了强制出现一组不同的记录。

于 2012-06-26T21:46:48.927 回答