Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-06-12 : 10:30:20
|
| Hi,I have the follwing table structuredeclare @pol table (polnum varchar(8),code varchar(2))insert @polselect 'L1234567','02'declare @rel table (rela_key varchar(20),nameid int)insert @relselect '02L1234567IN',1 union allselect '02L1234567IN',2declare @name table (nameid int,custname varchar(20))insert @nameselect 1,'name1' union allselect 2,'name2'And this is my queryselect p.polnum,p.code,n.custname from @pol p left join @rel r on r.rela_key = p.code + p.polnum + 'IN'left join @name n on n.nameid = r.nameidAs you might have guessed, it returns two rows for name1 and name2.The problem here is, the @rel table has 2 rela_key's with the same value. This is an invalid scenario. This duplicate record comes in through the system through some erroneous online processing, which we are not able to figure out yet.This SQL is part of a report, so the same policy number appears twice in the report which causes some confusion. Can we somehow alter the query so that the JOIN wud return only one row and leave out the other. It doesn't matter which one is being ignored. |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-06-12 : 10:46:21
|
Here are two ways:select p.polnum ,p.code ,n.custname from @pol pleft join (select rela_key, min(nameid) as nameid from @rel group by rela_key) r on r.rela_key = p.code + p.polnum + 'IN'left join @name n on n.nameid = r.nameidselect p.polnum ,p.code ,oa.custname from @pol pouter apply ( select top 1 n.custname from @rel r inner join @name n on n.nameid = r.nameid where r.rela_key = p.code + p.polnum + 'IN' order by r.nameid ) oa EDIT:a good database design will include constraints to prevent duplicate data. (Primary Key or Unique Constraint)Also it will have referential integrity like Foreign Keys to avoid orphaned child rows.Be One with the OptimizerTG |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-06-12 : 11:05:54
|
| I concur, but unfortunately this design came with a product that was purchased and that is one of the primary reasons why we couldn't find the reason for these errors yet. We are yet to get used to their code.The MIN() seems like a good and simple idea. Never struck me! |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-06-13 : 02:31:59
|
| try this tooselect p.polnum ,p.code ,n.custname from @pol pleft join (select *,row_number()over(partition by rela_key order by rela_key) as rid from @rel ) r on r.rela_key = p.code + p.polnum + 'IN' and rid = 1left join @name n on n.nameid = r.nameid |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-14 : 02:03:50
|
quote: Originally posted by vijayisonly Hi,I have the follwing table structuredeclare @pol table (polnum varchar(8),code varchar(2))insert @polselect 'L1234567','02'declare @rel table (rela_key varchar(20),nameid int)insert @relselect '02L1234567IN',1 union allselect '02L1234567IN',2declare @name table (nameid int,custname varchar(20))insert @nameselect 1,'name1' union allselect 2,'name2'And this is my queryselect p.polnum,p.code,n.custname from @pol p left join @rel r on r.rela_key = p.code + p.polnum + 'IN'left join @name n on n.nameid = r.nameidAs you might have guessed, it returns two rows for name1 and name2.The problem here is, the @rel table has 2 rela_key's with the same value. This is an invalid scenario. This duplicate record comes in through the system through some erroneous online processing, which we are not able to figure out yet.This SQL is part of a report, so the same policy number appears twice in the report which causes some confusion. Can we somehow alter the query so that the JOIN wud return only one row and leave out the other. It doesn't matter which one is being ignored.
so you want a single record per customer or just a single record with single customer? |
 |
|
|
|
|
|
|
|