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)
 left join shows record twice

Author  Topic 

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2009-11-04 : 07:59:53
SELECT dbo.vwtranscations.*,make,model,color
FROM dbo.vwtranscations left join customers on vwtranscations.telephone=customers.mobilenumber


this shows more then one record for each row in vwtranscations
why?

i want to show the record from vwtranscations and the corresponding fields from customers for that same number

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-04 : 08:02:48
add some columns from customer to your select list and maybe you will see.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2009-11-04 : 09:49:36
what do you mean?
i see the problem is i have 2 customer records so it shows it twice
how can i get it to just look at the first record?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-04 : 10:17:06
Your question was: Why this shows more then one record for each row in vwtransaction?
My answer was: add some columns from customer to your select list and maybe you will see

And then you are asking: What do you mean?
That's real funny!

quote:
how can i get it to just look at the first record?

How can we decide what is the first record?



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2009-11-04 : 10:32:53
I thought a left join takes all from the first table with 1 matching form the second - how can I match it with only the last record from the second?
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-04 : 10:40:58
How can we decide what is the last record?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2009-11-04 : 10:48:56
based on highest customerid
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-04 : 10:55:17
Try this:
SELECT
dbo.vwtranscations.*,
make,
model,
color
FROM dbo.vwtranscations

left join (select mobilenumber, max(customerid) as customerid from customers group by mobilenumber)dt
on dt.mobilenumber = vwtranscations.telephone

left join customers c
on dt.customerid = c.customerid




No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -