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
 General SQL Server Forums
 New to SQL Server Programming
 Duplicates

Author  Topic 

paull
Yak Posting Veteran

50 Posts

Posted - 2009-11-24 : 09:28:48
Hi guys!
I cant see what I am doing wrong here!!
I have a query that picks up data from 5 tables and merges it into one "Final" table.. basically Insert into Final (URN, VAR1, VA2...)
select d.URN, d.VAR1, m.VAR2, v.VAR3 etc etc)
from data d, model m, verbs v
where d.URN = m.URN etc etc
plus I only want certain ID that are stored in another table Keeps k
so "and k.URN=d.URN.
This has worked fine in the past but now I am getting a lot of duplicates in the Final table. Can anyone see where I am might be going wrong (its driving me mad!) and, would it be better / more efficient to use a sub query (you'll have to excuse me as I am still learning!) something like "in (select URN from Keeps)" Please correct me if I'm wrong!
Many many thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-24 : 10:09:20
1.) We cannot see your "real" select statement, so it is not easy to say what happens.
2.) Run that select without the insert-part and have a look at the joined data. Often there is the reason of dups.
3.) Have a look at your destination table - maybe some records are already there...

4.) Don't join like that from table1, table2, ... better use from table1 join table2 on ...



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

paull
Yak Posting Veteran

50 Posts

Posted - 2009-11-24 : 10:16:31
Hi Webfred
Thanks for the tip of running it without the Insert - I found where the dupes are coming from!!
Is there a reason not to join as I have, speed, accuracy etc, this query has developed and evolved since I started learning SQL so I am keen to update it where I should and use better code- I find it a good way to learn!!
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-24 : 10:32:32
Using the "JOIN-Syntax" is much better readable.
You can see the join conditions direct in relation to the joined tables.

Joining by table1, table2, ... will be deprecated somewhere in the future...


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

- Advertisement -