Help with query, dup results showing

I need help with the following query.

The first two display all results from two separate tables, the third query is a join but displays duplicate results, how can I prevent this from happening ?

Below are the three queries and attached a screenshot of the results.

---- first table (BillPaymntsRecvd) -----


SELECT  a.PaymntId ,
        a.CaseId ,
        a.BillsMainId ,
        a.ItemId ,
        a.ItemDesc ,
        a.PmtRecd ,
        a.Balance
FROM    dbo.BillPaymntsRecvd a
WHERE caseid = 11064
ORDER BY a.PaymntId DESC

---- second table (BillPaymentsRecvdTA) -----


SELECT  a.PaymntidTA ,
        a.Caseid ,
        a.BillsMainid ,
        a.ItemId ,
        a.ItemDesc ,
        a.PmtRecd ,
        a.Balance
FROM    dbo.BillPaymentsRecvdTA a
WHERE caseid = 11064
ORDER BY a.PaymntIdTA DESC

--- Query with JOIN and duplicates ---

SELECT  a.PaymntId ,
        a.CaseId ,
        a.BillsMainId ,
        a.ItemId ,
        a.ItemDesc ,
        a.PmtRecd ,
        a.Balance
FROM    dbo.BillPaymntsRecvd a
        INNER JOIN dbo.BillPaymentsRecvdTA AS b ON a.ItemId = b.ItemId
WHERE   a.caseid = 11064
        AND a.Balance <> 0
        ORDER BY a.PaymntId DESC
LVL 1
AleksAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
If the relationship from both tables aren't 1 to 1 (one to one) then you'll have duplicate records.
Depending on the database model you may resolve this issue with the DISTINCT keyword:
SELECT  DISTINCT a.PaymntId ,
         a.CaseId ,
         a.BillsMainId ,
         a.ItemId ,
         a.ItemDesc ,
         a.PmtRecd ,
         a.Balance 
FROM    dbo.BillPaymntsRecvd a
      INNER JOIN dbo.BillPaymentsRecvdTA AS b ON a.ItemId = b.ItemId
WHERE   a.caseid = 11064 AND a.Balance <> 0
ORDER BY a.PaymntId DESC 

Open in new window


If this doesn't resolve the issue please provide samples of data for all three queries.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slubekCommented:
You don't use any values from dbo.BillPaymentsRecvdTA AS b, so in my opinion it is not necessery to use JOIN in that query.
If you have multiple values of dbo.BillPaymentsRecvdTA.ItemID which have to be joined with caseid=11064, you get multiple records from dbo.BillPaymntsRecvd.
So:
1. remove Join
2. Use

group by  a.PaymntId ,
        a.CaseId ,
        a.BillsMainId ,
        a.ItemId ,
        a.ItemDesc ,
        a.PmtRecd ,
        a.Balance 

Open in new window


clause.
Shaun KlineLead Software EngineerCommented:
If there are duplicates appearing then it is highly likely that there are either multiple records in the two tables with the same ItemID. Can you verify that you are using the correct field to join the two tables? Just a quick glance at the first two queries, it would appear that either PaymntID, BillsMainID or CaseID should be also in the join statement.
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

AleksAuthor Commented:
I may have pasted the wrong query. I AM using the b.name

If I used DISTINCT I get no results. Where only the exact same should not be duplicated.

----

SELECT  a.PaymntId ,
        a.CaseId ,
        a.BillsMainId ,
        a.EntryBy ,
        a.ItemId ,
        a.ItemDesc ,
        a.ItemLongDec ,
        a.UnitCost ,
        a.Qty ,
        a.PmtRecd ,
        a.Balance ,
        a.apymntdate ,
        a.firmid ,
        b.PaymntidTA ,
        b.ItemId AS ItemidTA
FROM    dbo.BillPaymntsRecvd a
        INNER JOIN dbo.BillPaymentsRecvdTA AS b ON a.ItemId = b.ItemId
WHERE   a.BillsMainId = 1416
        AND a.Balance <> 0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Can you provide sample data?
Shaun KlineLead Software EngineerCommented:
If you are filtering on a.BillsMainID = 1416, and BillsMainID is also in BillPaymentsRecvdTA, would you not either need to use that field in your join statement or filter on that field in BillPaymentsRecvdTA as well?
AleksAuthor Commented:
I am trying to display payments, its the same payment in two tables, I won't get into the specifics of why I am doing this but we do need to enter the same payment in both tables, the ID's are different though, but I only want to display the payment from one of the tables BUT I need to pass on to the next page both "ID's" because the next page will update the values in both tables.

So .. how do I JOIN the tables so I can have both ID's while displaying the payment information only once ?  So that if the ItemID is the same on both tables it should display it only once regardless of  'paymntidTA' being different.

----

This are the correct queries:

SELECT  a.PaymntId ,
        a.CaseId ,
        a.BillsMainId ,
        a.ItemId ,
        a.ItemDesc ,
        a.PmtRecd ,
        a.Balance
FROM    dbo.BillPaymntsRecvd a
WHERE a.BillsMainId = 1423
ORDER BY a.PaymntId DESC

SELECT  a.PaymntidTA ,
        a.Caseid ,
        a.BillsMainid ,
        a.ItemId ,
        a.ItemDesc ,
        a.PmtRecd ,
        a.Balance
FROM    dbo.BillPaymentsRecvdTA a
WHERE a.BillsMainId = 1423
ORDER BY a.PaymntIdTA DESC

SELECT  
            a.PaymntId ,
        a.CaseId ,
        a.BillsMainId ,
        a.EntryBy ,
        a.ItemId ,
        a.ItemDesc ,
        a.ItemLongDec ,
        a.UnitCost ,
        a.Qty ,
        a.PmtRecd ,
        a.Balance ,
        a.apymntdate ,
        a.firmid ,
        b.PaymntidTA ,
        b.ItemId AS ItemidTA
FROM    dbo.BillPaymntsRecvd a
        INNER JOIN dbo.BillPaymentsRecvdTA AS b ON a.ItemId = b.ItemId
WHERE   a.BillsMainId = 1423
        AND a.Balance <> 0

--- The third one displays a duplicate because it displays the entry from each table.
results.gif
AleksAuthor Commented:
This is the results from all 3 queries
All-results.gif
Vitor MontalvãoMSSQL Senior EngineerCommented:
This is the results from all 3 queries
I'm wondering why is missing one record in the 2nd query (PaymntidTA = 207)
AleksAuthor Commented:
It should be one result from table BillPaymntsRecvd

It should be one result from table PaymntIdTA

Is there a way to have the join so that I have both "ID's" in the resulting join but only ONE result ?

Something like:

Paymntid, PaymntidTA, ... the rest of the entries which are identical in both tables

One record with both ID's instead of two records.
Vitor MontalvãoMSSQL Senior EngineerCommented:
Anyway, the only solution for returning a single row it's to have PaymntidTA separated by commas or another separator character (207, 209).
It's that an acceptable solution for you?
AleksAuthor Commented:
Actually ... it should only display the latest PaymntidTA .. can we try that instead ?  
Only the 209 and not the 207 ... this might be the issue here, its taking also the value of a previous paymentidTA and also displaying it
Vitor MontalvãoMSSQL Senior EngineerCommented:
That's what I was wondering about. Why 207 it's returned if it isn't on the 2nd query?
AleksAuthor Commented:
Correct, it should not be there, sounds like that is the issue, can we filter it so that it only shows the last (top) entry of the PaymntidTA ?
Shaun KlineLead Software EngineerCommented:
Tacking on to Vitor's question, in the 3rd query, include the BillMainID and Case ID from table B. It would seem that these will not match the values you are expecting.
AleksAuthor Commented:
I want to display the third query but only the result where the PaymntidTA is the last entry (highest value)
Vitor MontalvãoMSSQL Senior EngineerCommented:
Try this query:
SELECT  a.PaymntId ,
         a.CaseId ,
         a.BillsMainId ,
         a.EntryBy ,
         a.ItemId ,
         a.ItemDesc ,
         a.ItemLongDec ,
         a.UnitCost ,
         a.Qty ,
         a.PmtRecd ,
         a.Balance ,
         a.apymntdate ,
         a.firmid ,
         b.PaymntidTA,
         b.ItemId AS ItemidTA
 FROM    dbo.BillPaymntsRecvd a
    INNER JOIN dbo.BillPaymentsRecvdTA AS b ON a.ItemId = b.ItemId
 WHERE   a.BillsMainId = 1416
  AND a.Balance <> 0 
  AND b.PaymntidTA = (SELECT TOP 1 b.PaymntidTA 
    					FROM	BillPaymentsRecvdTA  ta
    					WHERE	a.ItemId = ta.ItemId
    					ORDER BY ta.PaymntidTA DESC) 

Open in new window

AleksAuthor Commented:
Did not return anything :$
AleksAuthor Commented:
Never mind, I was using the wrong ID, it in fact still returned 2 records.
AleksAuthor Commented:
Screenshot
qry.gif
Vitor MontalvãoMSSQL Senior EngineerCommented:
Both records has the same PaymntidTA value?
If so you need to add the DISTINCT keyword.
AleksAuthor Commented:
That's the whole thing, the ID is different. I need to display the result that has the latest entry (Highest ID)
Vitor MontalvãoMSSQL Senior EngineerCommented:
That's weird. Something must be missing. Please run the following two queries and tell me what returns, ok?
SELECT  TOP 1 b.PaymntidTA,
 FROM    dbo.BillPaymntsRecvd a
    INNER JOIN dbo.BillPaymentsRecvdTA AS b ON a.ItemId = b.ItemId
 WHERE   a.BillsMainId = 1416 AND a.Balance <> 0 
ORDER BY b.PaymntidTA DESC


SELECT  MAX(b.PaymntidTA)
 FROM    dbo.BillPaymntsRecvd a
    INNER JOIN dbo.BillPaymentsRecvdTA AS b ON a.ItemId = b.ItemId
 WHERE   a.BillsMainId = 1416   AND a.Balance <> 0 

Open in new window

AleksAuthor Commented:
I am going to close this and take a different approach. The above gives me two results. I will put the latest ID in a session and filter it that way see if that works.

Thanks !
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.