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.
Author |
Topic |
erwine
Yak Posting Veteran
69 Posts |
Posted - 2006-09-17 : 22:41:11
|
Hi,I would like to know if there is any way for me to compare rows in a table.create table TEST( [code] [int],[name] [nvarchar] (100),[address] [nvarchar] (200),[city] [nvarchar] (100))insert into TESTselect 1, 'ann', 'fifth ave', 'newyork'union allselect 2, 'ann', 'fifth ave', 'spore'based on the data, I would like to have result:difference----------codecitythanks for your kind attention.regards,erwine... sql is fun... |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-17 : 23:02:41
|
Will this do ?create table TEST( [code] [int], [name] [nvarchar] (100), [address] [nvarchar] (200), [city] [nvarchar] (100))insert into TESTselect 1, 'ann', 'fifth ave', 'newyork' union allselect 2, 'ann', 'fifth ave', 'spore' union allselect 3, 'bob', 'sixth ave', 'spore' union allselect 4, 'bob', 'sixth ave', 'newyork'select *from TEST t1 inner join TEST t2 on t1.name = t2.namewhere t1.code <> t2.codeor t1.address <> t2.addressor t1.city <> t2.citydrop table TEST KH |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-17 : 23:07:51
|
or this ?create table TEST( [code] [int], [name] [nvarchar] (100), [address] [nvarchar] (200), [city] [nvarchar] (100))insert into TESTselect 1, 'ann', 'fifth ave', 'newyork' union allselect 2, 'ann', 'fifth ave', 'spore' union allselect 3, 'bob', 'sixth ave', 'spore' union allselect 4, 'bob', 'sixth aves', 'spore'select t1.name, 'code' as difffrom TEST t1 inner join TEST t2 on t1.name = t2.namewhere t1.code <> t2.codegroup by t1.nameunion allselect t1.name, 'address'from TEST t1 inner join TEST t2 on t1.name = t2.namewhere t1.address <> t2.addressgroup by t1.nameunion allselect t1.name, 'city' from TEST t1 inner join TEST t2 on t1.name = t2.namewhere t1.city <> t2.citygroup by t1.name/*name diff ------ ------- ann codebob codebob addressann city(4 row(s) affected)*/drop table TEST KH |
 |
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2006-09-18 : 00:00:44
|
Hi KHTan,Thanks for your replies.For your information, my table has more than 50columns, this solution will cost me more time and I also need to modify the codes if the structure of table is changed. Please understand, I'm not saying your codes are not good, it's simply because it doesnt suit my case.Do you have more dynamic solution?regards,erwine... sql is fun... |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-18 : 00:19:34
|
Will the 1st query work for you ? or You required output as in the 2nd query i posted ? KH |
 |
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2006-09-18 : 01:39:47
|
Hi KHTan,The second query is closer to what I want.Basically, I only need to know which column(s) were changed.Thanks.regards,erwine... sql is fun... |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-18 : 01:53:40
|
"my table has more than 50columns, this solution will cost me more time and I also need to modify the codes if the structure of table is changed. "You can consider using Dynamic SQL to do it. Use INFORMATION_SCHEMA.COLUMNS to obtain the column names.For more information on Dynamic SQL, refer tohttp://www.sommarskog.se/dynamic_sql.html KH |
 |
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2006-09-18 : 02:02:51
|
Yes.Thanks for the link.regards,erwine... sql is fun... |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
erwine
Yak Posting Veteran
69 Posts |
Posted - 2006-09-20 : 06:06:38
|
"Why do you want this type of comparison?"I need it because it will give me the information of which column(s)(data) that is last updated. I'm not comparing a table, but I actually want to compare 2 rows(the last and the second last).Have you done this before, Madhivanan?Thanks for your attention.regards,erwine... sql is fun... |
 |
|
|
|
|
|
|