Solved

Help with query, dup results showing

Posted on 2014-10-20
24
116 Views
Last Modified: 2014-10-20
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
0
Comment
Question by:amucinobluedot
  • 12
  • 8
  • 3
  • +1
24 Comments
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 317 total points
Comment Utility
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.
0
 
LVL 7

Assisted Solution

by:slubek
slubek earned 46 total points
Comment Utility
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.
0
 
LVL 25

Assisted Solution

by:Shaun Kline
Shaun Kline earned 137 total points
Comment Utility
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.
0
 

Author Comment

by:amucinobluedot
Comment Utility
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
0
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 317 total points
Comment Utility
Can you provide sample data?
0
 
LVL 25

Assisted Solution

by:Shaun Kline
Shaun Kline earned 137 total points
Comment Utility
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?
0
 

Author Comment

by:amucinobluedot
Comment Utility
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
0
 

Author Comment

by:amucinobluedot
Comment Utility
This is the results from all 3 queries
All-results.gif
0
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 317 total points
Comment Utility
This is the results from all 3 queries
I'm wondering why is missing one record in the 2nd query (PaymntidTA = 207)
0
 

Author Comment

by:amucinobluedot
Comment Utility
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.
0
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 317 total points
Comment Utility
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?
0
 

Author Comment

by:amucinobluedot
Comment Utility
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
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 317 total points
Comment Utility
That's what I was wondering about. Why 207 it's returned if it isn't on the 2nd query?
0
 

Author Comment

by:amucinobluedot
Comment Utility
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 ?
0
 
LVL 25

Assisted Solution

by:Shaun Kline
Shaun Kline earned 137 total points
Comment Utility
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.
0
 

Author Comment

by:amucinobluedot
Comment Utility
I want to display the third query but only the result where the PaymntidTA is the last entry (highest value)
0
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 317 total points
Comment Utility
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

0
 

Author Comment

by:amucinobluedot
Comment Utility
Did not return anything :$
0
 

Author Comment

by:amucinobluedot
Comment Utility
Never mind, I was using the wrong ID, it in fact still returned 2 records.
0
 

Author Comment

by:amucinobluedot
Comment Utility
Screenshot
qry.gif
0
 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 317 total points
Comment Utility
Both records has the same PaymntidTA value?
If so you need to add the DISTINCT keyword.
0
 

Author Comment

by:amucinobluedot
Comment Utility
That's the whole thing, the ID is different. I need to display the result that has the latest entry (Highest ID)
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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

0
 

Author Comment

by:amucinobluedot
Comment Utility
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 !
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now