Query join of table duplicates records

Adding the following join duplicates every record doubling the number of records in the below complicated query.  Any ideas to resolve this? I tried DISTINCTROW and changing the Inner Join to a Left Join.

INNER JOIN tblPatientProviderList ON [Complaint List].ComplaintID = tblPatientProviderList.ComplaintID) ON ReferDr.ContactID = tblPatientProviderList.ContactID)

QUERY:
SELECT
      DISTINCT
      Left(IIf([Ledger.Status]=1,
      [Ins1Info.CompanyName],
      [Ins2Info.CompanyName]),6) & Format([Provider],
      '00') & [Last Name] & [Visit.ComplaintID] & [Ledger.Status] AS GroupField,
      [CPT code list].CPTCode,
      BillingProvider.ContactID,
      [Complaint List].MVA,
      [Complaint List].MVAState,
      [Complaint List].WC,
      [Complaint List].OtherAccident,
      IIf([Ins1GroupInfo.Group Number]='None',' ',
      [Ins1GroupInfo.Group Number]) AS Group1Number,
      Ins1GroupInfo.InsuranceType,
      Ins1GroupInfo.PrintClaim,
      IIf([Ins2GroupInfo.Group Number]='None',' ',
      [Ins2GroupInfo.Group Number]) AS Group2Number,
      Ins2GroupInfo.InsuranceType,
      Ins2GroupInfo.PrintClaim,
      Ins1Info.SubmitterID,
      Ins1Info.CompanyName,
      Ins1Info.Address,
      Ins1Info.City,
      Ins1Info.State,
      Ins1Info.Zip,
      Ins1Info.InsRef,
      Ins2Info.SubmitterID,
      Ins2Info.CompanyName,
      Ins2Info.Address,
      Ins2Info.City,
      Ins2Info.State,
      Ins2Info.Zip,
      Ins2Info.InsRef,
      Ins1PatientInfo.InsuredFName,
      Ins1PatientInfo.InsuredMName,
      Ins1PatientInfo.InsuredLName,
      Ins1PatientInfo.Address,
      Ins1PatientInfo.City,
      Ins1PatientInfo.State,
      Ins1PatientInfo.Zip,
      Ins1PatientInfo.Phone,
      Ins1PatientInfo.InsuredDOB,
      Ins1PatientInfo.InsuredRelation,
      Ins2PatientInfo.InsuredFName,
      Ins2PatientInfo.InsuredMName,
      Ins2PatientInfo.InsuredLName,
      Ins2PatientInfo.Address,
      Ins2PatientInfo.City,
      Ins2PatientInfo.State,
      Ins2PatientInfo.Zip,
      Ins2PatientInfo.Phone,
      Ins2PatientInfo.InsuredDOB,
      Ins2PatientInfo.InsuredRelation,
      Ledger.LedgerID,
      Visit.DOS,
      Ledger.CPTID,
      Ledger.Charge,
      Ledger.Balance,
      Ledger.Quantity,
      Ledger.Mod1,
      Ledger.Mod2,
      Ledger.Mod3,
      Visit.ICD1,
      Visit.ICD2,
      Visit.ICD3,
      Visit.ICD4,
      Ledger.Status,
      Ledger.Use1,
      Ledger.Use2,
      Ledger.Use3,
      Ledger.Use4,
      Visit.Ins1Policy,
      Visit.Ins2Policy,
      Ledger.Ins1Pmt,
      Visit.PatientID,
      Ledger.PatientPmt,
      Visit.Provider,
      Visit.Technician,
      Visit.PAnumber,
      [Patient Information].State,
      [Patient Information].[Postal Code],
      [Patient Information].Sex,
      [Patient Information].[Home Phone],
      [Patient Information].Birthdate,
      [Patient Information].[Last Name],
      [Patient Information].[First Name],
      [Patient Information].[Middle Initial],
      [Patient Information].Address,
      [Patient Information].City,
      [Patient Information].[Marital Status],
      [Patient Information].Employer,
      Provider.FirstName,
      Provider.LastName,
      Provider.CompanyName,
      Provider.Address,
      Provider.City,
      Provider.StateOrProvince,
      Provider.PostalCode,
      Provider.WorkPhone,
      Provider.Suffix,
      Provider.UPIN AS ProvUPIN,
      Provider.NPI,
      ReferDr.FirstName,
      ReferDr.LastName,
      ReferDr.UPIN,
      ReferDr.NPI,
      ReferDr.Suffix,
      [Resource List].Initials,
      [Resource List].ProviderType,
      Ledger.Desc,
      Ledger.Ins2Pmt,
      Visit.VisitID,
      [Complaint List].PlaceOfService,
      [Complaint List].TypeOfService,
      IIf([PlaceOfService]>'20' AND
      [PlaceOfService]<'39',
      [FirstVisit],
      '') AS AdmDate,
      IIf([PlaceOfService]>'20' AND
      [PlaceOfService]<'39',
      [LastVisit],
      '') AS LastDate,
      Facility.CompanyName,
      Facility.Address,
      Facility.City,
      Facility.StateOrProvince,
      Facility.PostalCode,
      Facility.NPI,
      tblPatientProviderList.DescriptionID
FROM
      (((Contacts AS ReferDr
      INNER JOIN ((([Patient Information]
      INNER JOIN (((((Ledger
      LEFT JOIN [CPT code list] ON Ledger.CPTID = [CPT code list].CPTID)
      INNER JOIN ((((([Complaint List]
      LEFT JOIN [Insurance Group Information] AS Ins1GroupInfo ON [Complaint List].Group1 = Ins1GroupInfo.GroupID)
      LEFT JOIN [Insurance Group Information] AS Ins2GroupInfo ON [Complaint List].Group2 = Ins2GroupInfo.GroupID)
      LEFT JOIN [Insurance Patient Information] AS Ins1PatientInfo ON [Complaint List].Ins1PTID = Ins1PatientInfo.InsPTID)
      LEFT JOIN [Insurance Patient Information] AS Ins2PatientInfo ON [Complaint List].Ins2PTID = Ins2PatientInfo.InsPTID)
      INNER JOIN Visit ON [Complaint List].ComplaintID = Visit.ComplaintID) ON Ledger.VisitID = Visit.VisitID)
      LEFT JOIN [Insurance Companies] AS Ins1Info ON Visit.Ins1ID = Ins1Info.InsCoID)
      LEFT JOIN [Insurance Companies] AS Ins2Info ON Visit.Ins2ID = Ins2Info.InsCoID)
      LEFT JOIN (Contacts AS Provider
      RIGHT JOIN [Resource List] ON Provider.ContactID = [Resource List].ContactID) ON Visit.Provider = [Resource List].ResourceID) ON [Patient Information].PatientID = Visit.PatientID)
      LEFT JOIN Contacts AS Facility ON [Complaint List].Facility = Facility.ContactID)
      INNER JOIN tblPatientProviderList ON [Complaint List].ComplaintID = tblPatientProviderList.ComplaintID) ON ReferDr.ContactID = tblPatientProviderList.ContactID)
      INNER JOIN tblProviderDescription ON [Complaint List].ComplaintType = tblProviderDescription.BillingProviderType)
      INNER JOIN tblPatientProviderList AS tblPatientProviderList_1 ON (tblProviderDescription.ProviderDescriptionID = tblPatientProviderList_1.DescriptionID) AND
      ([Complaint List].ComplaintID = tblPatientProviderList_1.ComplaintID))
      INNER JOIN Contacts AS BillingProvider ON tblPatientProviderList_1.ContactID = BillingProvider.ContactID
WHERE
      (((Ledger.LedgerID) In (2647,
      2810,
      2825,
      2893,
      2507,
      2993,
      2332,
      2488,
      2503,
      2529,
      2569,
      2723,
      2787,
      2887,
      3368,
      3834,
      3520,
      3567,
      1407,
      914,
      913,
      916,
      1079,
      1084,
      1088,
      1286,
      1310,
      1326,
      1335,
      1334,
      1270,
      1264,
      1253,
      1233,
      1625,
      1635,
      1644,
      1652,
      2860,
      2928,
      2955,
      2961,
      2968,
      2975,
      2982,
      2998,
      3010,
      1658,
      1945,
      1980,
      1990,
      2002,
      2013,
      2024,
      2156,
      2164,
      2172,
      1899,
      3775,
      3528,
      3648,
      3902,
      3911,
      3913,
      3727,
      3724,
      3933,
      3936,
      3938,
      3940,
      3946,
      3721,
      3685,
      3675,
      2343,
      2396,
      783,
      388,
      3903,
      3904,
      3905,
      3915,
      3918,
      3726,
      3723,
      3948,
      3703,
      3680,
      3672,
      3678,
      3661,
      1907,
      2345,
      2399,
      2425,
      2435,
      3848,
      3784,
      3845,
      2531,
      2570,
      3507,
      3555,
      3385)))
ORDER BY
      Left(IIf([Ledger.Status]=1,
      [Ins1Info.CompanyName],
      [Ins2Info.CompanyName]),
      6) & Format([Provider],
      '00') & [Last Name] & [Visit.ComplaintID] & [Ledger.Status];
LVL 39
thenelsonAsked:
Who is Participating?
 
peter57rConnect With a Mentor Commented:
Distinct looks at every field in your Select clause.
Are you saying that you have duplicates of entire rows in your output - I suspect that's not what you mean.

If you have 'duplicates' based on some of the fields then you have to look at the tabl(s) that are contributing multiple rows for the same duplicate values.  For each table of this type, you need to create a sub-query to select just the unique records you want . Then use the subqueries instead of the tables in your main query.
0
 
thenelsonAuthor Commented:
I found the answer. I need to add "AND ((tblPatientProviderList.DescriptionID)=2)" to the Where clause since the added join added two different sets of valves and I only wanted one of them.

Although I got the solution before reading your comment, I'm going to give you the points since your suggestion is right on and would have led me to the solution if I didn't find it myself.  Thanks for the reply.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.