Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"Because of this forum, I continue to WOW! my clients!"

Geography

Where in the world do Tek-Tips members come from?
dvannoy (MIS)
24 May 12 15:13
I'm having an issue with a join and duplicating records. I can't seem to figure this out.

Any help would be appreciated.

Thanks in advance

----------------------------------------------

SELECT DISTINCT DOCUMENT, INVOICENO, INVOICEDATE, COST, SELL, SEQ, X
FROM BilledInvoices
WHERE (DOCUMENT = '000039111')
ORDER BY DOCUMENT, SEQ

-----------------------------------------------

Shows the Following with is correct - There are 4 records

DOCUMENT INVOICENO INVOICEDATE COST SELL SEQ X
-----------------------------------------------------------------------
000039111 123456 01/01/12 1.00 2.00 1 X
000039111 123456 01/01/12 2.00 3.00 2 X
000039111 123456 01/01/12 3.00 4.00 3 X
000039111 123456 01/01/12 4.00 5.00 4 X




Now, I want to join the above table with another table to get a final code number associated with each line item. Query is below

SELECT DISTINCT
BilledInvoices.DOCUMENT, BilledInvoices.INVOICENO, BilledInvoices.INVOICEDATE, BilledInvoices.COST, BilledInvoices.SELL,
BilledInvoices.SEQ, BilledInvoices.X, DISP.FINALCODE
FROM BilledInvoices INNER JOIN
DISP ON BilledInvoices.DOCUMENT= DISP.DOCUMENT
WHERE (BilledInvoices.DOCUMENT= '000039111')
ORDER BY BilledInvoices.DOCUMENT, BilledInvoices.SEQ


Now, this query returns 16 records instead of the 4. how can I eliminate the duplicate records ? There should only be 4 records total.



Thanks

Helpful Member!  JonFer (Programmer)
24 May 12 15:27
Can you show the rows from DISP where DOCUMENT = '000039111' ?

You will probably need to either: 1) add another field in the join or 2) join to a derived table (instead of DISP) which selects one row per DOCUMENT from DISP.
dvannoy (MIS)
24 May 12 15:30
Here is what I would like it to look like...

DOCUMENT INVOICENO INVOICEDATE COST SELL SEQ X DISP
000039111 123456 01/01/12 1.00 2.00 1 X 1000
000039111 123456 01/01/12 2.00 3.00 2 X 1001
000039111 123456 01/01/12 3.00 4.00 3 X 1002
000039111 123456 01/01/12 4.00 5.00 4 X 1003


--------------------------------------------------------------------------------------

Here is what I'm getting after the join

DOCUMENT INVOICENO INVOICEDATE COST SELL SEQ X DISP
000039111 123456 01/01/12 1.00 2.00 1 X 1000
000039111 123456 01/01/12 2.00 3.00 2 X 1001
000039111 123456 01/01/12 3.00 4.00 3 X 1002
000039111 123456 01/01/12 4.00 5.00 4 X 1003
000039111 123456 01/01/12 1.00 2.00 1 X 1000
000039111 123456 01/01/12 2.00 3.00 2 X 1001
000039111 123456 01/01/12 3.00 4.00 3 X 1002
000039111 123456 01/01/12 4.00 5.00 4 X 1003
000039111 123456 01/01/12 1.00 2.00 1 X 1000
000039111 123456 01/01/12 2.00 3.00 2 X 1001
000039111 123456 01/01/12 3.00 4.00 3 X 1002
000039111 123456 01/01/12 4.00 5.00 4 X 1003
000039111 123456 01/01/12 1.00 2.00 1 X 1000
000039111 123456 01/01/12 2.00 3.00 2 X 1001
000039111 123456 01/01/12 3.00 4.00 3 X 1002
000039111 123456 01/01/12 4.00 5.00 4 X 1003

Helpful Member!  djj55 (Programmer)
24 May 12 15:33
what is duplicated in table DISP? can you use a second (AND) criteria for the join?

djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!

dvannoy (MIS)
24 May 12 15:38
sorry, the DISP field in the DISP table. once adding that field, I get the duplicates. I don't have another field currently to do a second AND in my join

dvannoy (MIS)
24 May 12 15:39
Sorry, I meant finalcode in the DISP table

Helpful Member!  mstrmage1768 (Programmer)
24 May 12 15:44
The problem is that you are joining to the DISP table, but your join is not tight enough. i.e. You need another column to join on to find the distinct record to join to. because the DISP table has four records with the same document number, the join will always result in multiple returns if you do not add another column. It looks to me like the SEQ column would be the best bet. Try something like:

SELECT DISTINCT
BilledInvoices.DOCUMENT, BilledInvoices.INVOICENO, BilledInvoices.INVOICEDATE, BilledInvoices.COST, BilledInvoices.SELL,
BilledInvoices.SEQ, BilledInvoices.X, DISP.FINALCODE
FROM BilledInvoices INNER JOIN
DISP ON BilledInvoices.DOCUMENT= DISP.DOCUMENT
AND BilledInvoices.SEQ = DISP.SEQ
WHERE (BilledInvoices.DOCUMENT= '000039111')
ORDER BY BilledInvoices.DOCUMENT, BilledInvoices.SEQ

If SEQ is not the correct column, then just like JonFor said earlier...we need to see what the COMPLETE layout and some sample/dummy data for the two tables to help you find the best join.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
C#.NET Programmer

dvannoy (MIS)
24 May 12 17:20
Thank you everyone for your help..

After looking a little at my table structure I was able to add a few more fields and get things linked correctly.

Thanks again

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close