何らかの理由で、クエリが重複行を返します。クエリを作成しようとしてい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