SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


help with joining to table with non unique index and duplicate rows


help with joining to table with non unique index and duplicate rows

Author
Message
plivingstone
plivingstone
SSC-Enthusiastic
SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)

Group: General Forum Members
Points: 157 Visits: 56
Hi,

I have the below query to join several tables, and retrieve customer data:

select sv.SVSEQ, sc.SCACO, ac.f1 as AccountNo, ne.NEAN, ne.NEEAN,bg.BGCFN1,bg.BGCFN2, bg.BGCFN3, bg.BGCUS,
sv.SVNA1 ,sv.SVNA2, sv.SVNA3, sv.SVNA4, sv.SVNA5, sv.SVPZIP,sv.SVCSA, sc.SCACO,sx.SXDLM
from NEPF ne
inner join tmpAccountList ac on NEEAN=ac.f1
inner join BGPF bg on bg.BGCUS=ne.NEAN
inner join SCPF sc on sc.SCAN=bg.BGCUS and bg.BGCUS=ne.NEAN and sc.SCAN=LEFT(ac.f1,6)
inner join sxpf sx on sx.SXCUS=LEFT(ac.f1,6)
INNER JOIN SVPF SV ON sv.SVDLM=sx.SXDLM



my issue is with the table SVPF which has thousands of duplicate records.

There is a column SVSEQ which is numbered, but not uniquely. I want to join to that table, but only get one row for each individual Sequence in my results set.

Without that table, I have 150 records, with that table, I have several hundred thousand.

How can I add that table using the SVSEQ column without getting duplicates?
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)SSC Guru (145K reputation)

Group: General Forum Members
Points: 145876 Visits: 13350
Can you post some sample data and desired output?


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96954 Visits: 20698
select sv.SVSEQ, sc.SCACO, ac.f1 as AccountNo, ne.NEAN, ne.NEEAN,bg.BGCFN1,bg.BGCFN2, bg.BGCFN3, bg.BGCUS,
sv.SVNA1 ,sv.SVNA2, sv.SVNA3, sv.SVNA4, sv.SVNA5, sv.SVPZIP,sv.SVCSA, sc.SCACO,sx.SXDLM
from NEPF ne
inner join tmpAccountList ac on NEEAN=ac.f1
inner join BGPF bg on bg.BGCUS=ne.NEAN
inner join SCPF sc on sc.SCAN=bg.BGCUS and bg.BGCUS=ne.NEAN and sc.SCAN=LEFT(ac.f1,6)
inner join sxpf sx on sx.SXCUS=LEFT(ac.f1,6)
INNER JOIN (
SELECT
SVSEQ, SVDLM, SVNA1, SVNA2, SVNA3, SVNA4, SVNA5, SVPZIP, SVCSA,
rn = ROW_NUMBER() OVER (PARTITION BY SVSEQ ORDER BY SVDLM)
FROM SVPF
) SV ON sv.SVDLM = sx.SXDLM AND rn = 1



Experiment by running the subquery on it's own to determine the best column to order by.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Erland Sommarskog
Erland Sommarskog
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13312 Visits: 879
Without knowledge of the table it is impossible to give a good answer. You can always do:

JOIN (SELECT *, 
row_number() OVER(PARTION BY SVSEQ ORDER BY (SELECT NULL)) AS rowno
FROM SVPF) AS SV ON SV.SVDLM = SX.SXDLM
AND SV.rowno = 1



It will reduced the number of rows, but it is very unlikely that the result is correct.

I think the correct answer is to speak with someone who knows these tables better to be able to find out which rows to present.

Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Cadavre
Cadavre
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22002 Visits: 8519
Another guess: -
SELECT sv.SVSEQ, sc.SCACO, ac.f1 AS AccountNo, 
ne.NEAN, ne.NEEAN, bg.BGCFN1, bg.BGCFN2,
bg.BGCFN3, bg.BGCUS, sv.SVNA1, sv.SVNA2,
sv.SVNA3, sv.SVNA4, sv.SVNA5, sv.SVPZIP,
sv.SVCSA, sc.SCACO, sx.SXDLM
FROM NEPF ne
INNER JOIN tmpAccountList ac ON NEEAN = ac.f1
INNER JOIN BGPF bg ON bg.BGCUS = ne.NEAN
INNER JOIN SCPF sc ON sc.SCAN = bg.BGCUS AND bg.BGCUS = ne.NEAN AND sc.SCAN = LEFT(ac.f1, 6)
INNER JOIN sxpf sx ON sx.SXCUS = LEFT(ac.f1, 6)
CROSS APPLY (
SELECT TOP 1 SVSEQ, SVDLM, SVNA1, SVNA2, SVNA3, SVNA4, SVNA5, SVPZIP, SVCSA
FROM SVPF a
WHERE a.SVDLM = sx.SXDLM
ORDER BY SVDLM
) SV;




Forever trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


Craig Wilkinson - Software Engineer
LinkedIn
plivingstone
plivingstone
SSC-Enthusiastic
SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)SSC-Enthusiastic (157 reputation)

Group: General Forum Members
Points: 157 Visits: 56
thanks everyone, will get to work
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search