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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need help on a LEFT JOIN

Author  Topic 

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-06-12 : 10:30:20
Hi,
I have the follwing table structure

declare @pol table (polnum varchar(8),code varchar(2))
insert @pol
select 'L1234567','02'

declare @rel table (rela_key varchar(20),nameid int)
insert @rel
select '02L1234567IN',1 union all
select '02L1234567IN',2

declare @name table (nameid int,custname varchar(20))
insert @name
select 1,'name1' union all
select 2,'name2'

And this is my query

select 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.nameid

As 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 p
left 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.nameid

select p.polnum
,p.code
,oa.custname
from @pol p
outer 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 Optimizer
TG
Go to Top of Page

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!
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-06-13 : 02:31:59
try this too
select p.polnum
,p.code
,n.custname
from @pol p
left 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 = 1
left join @name n
on n.nameid = r.nameid
Go to Top of Page

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 structure

declare @pol table (polnum varchar(8),code varchar(2))
insert @pol
select 'L1234567','02'

declare @rel table (rela_key varchar(20),nameid int)
insert @rel
select '02L1234567IN',1 union all
select '02L1234567IN',2

declare @name table (nameid int,custname varchar(20))
insert @name
select 1,'name1' union all
select 2,'name2'

And this is my query

select 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.nameid

As 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?
Go to Top of Page
   

- Advertisement -