[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Query join of table duplicates records

Posted on 2008-11-06
2
Medium Priority
?
275 Views
Last Modified: 2012-05-05
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];
0
Comment
Question by:thenelson
2 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 1500 total points
ID: 22897253
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
 
LVL 39

Author Comment

by:thenelson
ID: 22897379
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question