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
 Basic SQL help

Author  Topic 

kangaroo_m
Starting Member

9 Posts

Posted - 2011-02-03 : 12:36:51
I have 2 tables that hold data, table1 has 3k rows table2 has 9k. They both have a user_id feild. I want to combine the records that have the same user_id and also keep the records that dont(roughly 6k). I can find the matching records using;

SELECT table1.*, table2.*
FROM table1, table2
WHERE table1.user_id = table2.user_id

but it doesn't keep the remaining records that dont have the same user_id.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-03 : 14:01:39
You'll likely need an OUTER JOIN.

Can you post some sample data to make this more clear?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

kangaroo_m
Starting Member

9 Posts

Posted - 2011-02-03 : 18:51:06
table1
user_id...|...age...|...gendre
..101..........29..........M
..103..........46..........F

table2
user_id...|...date_joined...|...name
..101...........12/07/2008......Peter
..102...........17/03/2009......John
..103...........13/11/2010......Mary

Basically I want to add table1 to table2 where the user_id matches but keep the records that dont match aswell. I hope this has made it clearer, I am not very good at explaining myself sometimes.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-03 : 18:56:39
SELECT *
FROM table2 t2
LEFT JOIN table1 t1 --this is an OUTER join
ON t1.user_id = t2.user_id

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

kangaroo_m
Starting Member

9 Posts

Posted - 2011-02-04 : 10:02:13
Worked perfectly, thank you.

Can I use SQL to check if any of the fields data is entireley duplicate data to another field? e.g.
table1
user_id...|...age...|...gendre...|...user_name...|...name..|..sex
..101..........29..........M..............Peter.............Peter......M
..102..........35..........M..............Spikey............John.......M
..103..........46..........F...............Mary..............Mary.......F

So I would like it to identify 'gendre' and 'sex' but not 'user_name' and 'name'
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2011-02-04 : 10:22:38
side question: why do you have an age column? won't it be out of date in a year? seems like it would be better to have a "birthdate" column and compute the age from that always, in a computed column if you really need an age column.


elsasoft.org
Go to Top of Page

kangaroo_m
Starting Member

9 Posts

Posted - 2011-02-04 : 12:21:56
Yes your right, I was just trying to simplify my table for this example, appologies for any confusion caused.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-04 : 12:24:37
WHERE gendre = sex

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

kangaroo_m
Starting Member

9 Posts

Posted - 2011-02-04 : 16:30:03
but I would like it to find entire columns that match not just individual records.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-04 : 16:30:59
You'll need to provide better sample data then plus expected output.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

kangaroo_m
Starting Member

9 Posts

Posted - 2011-02-05 : 17:08:50
I will get back to my drwing board and do my best to figure it out myself. Thanks for the help on the outer join.
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-02-05 : 18:07:02
quote:
I want to combine the records that have the same user_id and also keep the records that dont(roughly 6k).


What you want is a MERGE, for example. you would update records based on matching user_id, and insert those who don't. It can be made more complex and do more if needed, but the general idea below..

MERGE INTO table1 as t1
USING table2 as t2
on t1.user_id = t2.user_id
WHEN MATCHED THEN
UPDATE t1.name = t2.name,
t1.age = t2.age,
t1.sex = t2.gendre
WHEN NOT MATCHED THEN
INSERT ( user_id,
name,
age,
sex)
Values (t2.user_id,
t2.name,
t2.age,
t2.gendre)

You can do a derived query in the USING clause if needed to produce a complete record set...


*note: SQL 2K8 only.

you would have to do it in two steps using UPDATE and INSERT for prior versions.





Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page
   

- Advertisement -