Author |
Topic |
baja_yu
Starting Member
24 Posts |
Posted - 2010-10-13 : 17:13:05
|
Hey people. I've been banging my head trying to do something. Here's the setup. Let's say I have two tables, Table1 and Table2 with columns: ID (primary ID, autoincrementing), Name, Number. (and some other stuff that's not important)I have a set of data in both, mostly identical, but in Table2 the data in column Number changes for some items.Now, I have a third table with columns ID (primID, autoincr), PersonID, NewNumber.What I'd like is a query that will analyize Table2, and for each entry where Number from Table2 is different to Number in table1 (with same IDs), copy the ID of that item to PersonID and Number to NewNumber in the third (log) table.It it doable with one query? I've been trying with SELECT INTO but with not much luck. |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-10-13 : 17:29:51
|
insert table3 (PersonId,NewNumber)selectt2.ID,t2.Numberfrom table2 as t2join table1 as t1on t1.ID = t2.ID and t1.Number <> t2.Number No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
baja_yu
Starting Member
24 Posts |
Posted - 2010-10-13 : 17:49:11
|
Thanks for the replies.Both suggestions worked the same, but did not return the set of results I expected. Table1 has about 120K and Table2 133K records. The difference is in about 9K records. Both suggestions returned exactly 155.EDIT: The SELECT from returns 155 items but the join doubled all the columns. Since table1 and table2 have identical structure the JOIN returned each column twice, one original and one with _1 sufix. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-10-13 : 17:53:50
|
Then you must have a select that shows you the expected rows otherwise you can't say that our result is wrong.So take that select and use it for your insert. No, you're never too old to Yak'n'Roll if you're too young to die. |
|
|
baja_yu
Starting Member
24 Posts |
Posted - 2010-10-13 : 17:57:31
|
Here's a sample data setTable1 (ID - Name - Number)1 - Bob - 12 - Mike - 13 - Emma - NULL4 - John - 45 - Ed - 46 - Sean - NULLTable 21 - Bob - 12 - Mike - 13 - Emma - 44 - John - 45 - Ed - 1Changes were to items with ID 3 and 5 in table 2. So resulting table3 should beTable3 (ID - PersonID - NewNumber)1 - 3 - 42 - 5 - 1(ID is autogenerated here, PersonID and NewNumber are from Table2) |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2010-10-13 : 18:07:04
|
I added in an extra row into Table2 in case the Number column there can contain nulls too. A null is not equal to anything even another null, but I suspect in your situation it is.create table #Table1 (ID tinyint, Name varchar(10), Number tinyint)create table #Table2 (ID tinyint, Name varchar(10), Number tinyint)create table #Table3 (ID tinyint identity(1, 1), PersonID tinyint, NewNumber tinyint)insert into #Table1 values (1, 'Bob', 1)insert into #Table1 values (2, 'Mike', 1)insert into #Table1 values (3, 'Emma', null)insert into #Table1 values (4, 'John', 4)insert into #Table1 values (5, 'Ed', 4)insert into #Table1 values (6, 'Sean', null)insert into #Table2 values (1, 'Bob', 1)insert into #Table2 values (2, 'Mike', 1)insert into #Table2 values (3, 'Emma', 4)insert into #Table2 values (4, 'John', 4)insert into #Table2 values (5, 'Ed', 1)insert into #Table2 values (6, 'Sean', null)--select * from #Table1--select * from #Table2insert into #Table3 (PersonID, NewNumber)select t1.ID, t2.Numberfrom #table1 t1join #table2 t2on t1.ID = t2.IDwhere t1.Number <> t2.Number or (t2.Number IS NOT NULL AND t1.Number IS NULL)select * from #Table3drop table #Table1, #Table2, #Table3 Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
|
|
baja_yu
Starting Member
24 Posts |
Posted - 2010-10-13 : 18:12:55
|
Right on the money. Most differences in Table2 compared to Table1 are the NULL values in Table1. So the previous return of 155 was correct but did not include those. I expected to compare NULL to a number and get a positive result. I'll keep that in mind from now on.This works like a charm. Thanks a million once again :) |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|