Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

help with joining to table with non unique index and duplicate rows Expand / Collapse
Author
Message
Posted Friday, July 5, 2013 4:43 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, October 30, 2014 3:39 AM
Points: 11, Visits: 51
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?
Post #1470659
Posted Friday, July 5, 2013 4:54 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 12:52 PM
Points: 13,636, Visits: 11,509
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1470664
Posted Friday, July 5, 2013 4:56 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:43 AM
Points: 6,890, Visits: 14,254
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
Post #1470665
Posted Friday, July 5, 2013 5:00 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, December 12, 2014 1:02 AM
Points: 823, Visits: 753
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
Post #1470666
Posted Friday, July 5, 2013 5:05 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 1:30 AM
Points: 2,386, Visits: 7,622
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;




Not a DBA, just 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


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1470667
Posted Friday, July 5, 2013 7:40 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, October 30, 2014 3:39 AM
Points: 11, Visits: 51
thanks everyone, will get to work
Post #1470736
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse