Author |
Topic |
sebastian11c
Posting Yak Master
129 Posts |
Posted - 2012-01-13 : 09:23:39
|
hi there ,i have 2 tables "partner_info" and "my_company_info" --1st tableCREATE TABLE [dbo].[partner_info]( [general_id] [nchar](10) NULL, [office_partner] [varchar](50) NULL, [house_partner] [nchar](10) NULL, [service_partner] [varchar](50) NULL) ON [PRIMARY]--2nd tableCREATE TABLE [dbo].[My_company_info]( [general_id] [nchar](10) NULL, [co_id] [int] NULL, [office_co] [varchar](50) NULL, [house_co] [nchar](10) NULL, [service_co] [varchar](50) NULL) ON [PRIMARY]GO--now ill put some records in my tables my db its called "sura"INSERT INTO [sura].[dbo].[partner_info] ([general_id] ,[office_partner] ,[house_partner] ,[service_partner])VALUES ('c1','usa', 'miami', 'buy'),--dos registros('c1','canada', 'toronto', 'buy'),('c2','usa', 'miami', 'buy'),('c4','usa', 'miami', 'buy'),('c5','canada', 'toronto', 'buy'),('c6','usa', 'miami', 'buy'), ('c7','canada', 'toronto', 'buy'), ('c8','usa', 'miami', 'buy'), ('c9','canada', 'toronto', 'buy'),('c10','usa', 'miami', 'buy'), ('c11','canada', 'toronto', 'buy') INSERT INTO [sura].[dbo].[My_company_info] ([general_id] ,[co_id] ,[office_co] ,[house_co] ,[service_co]) VALUES('c1',1,'usa', 'miami', 'buy'),('c1',2,'canada', 'toronto', 'buy'),('c3',3,'canada', 'toronto', 'buy'),('c4',4,'canada', 'miami', 'buy'),('c5',5,'canada', 'otawa', 'buy'),('c6',6,'usa', 'miami', 'sell'), ('c7',7,'usa', 'NY', 'buy'), ('c8',8,'canada', 'miami', 'sell'), ('c9',9,'canada', 'otawa', 'sell'), ('c10',10, 'canada', 'toronto', 'sell'), ('c11',11,'canada', 'toronto', 'buy') and now, its when i really need your help i want to identify all the differences between these 2 tables , and show me the differences i need a query that gets this result, and show me the differences'c1' dont appear because the 2 records with this general_id in the 2 tables are exactly equals'c2' must appears because doesnt exist in my_company_info'c3' must appears because doesnt exist in partner_info table'c4' must appears because have different office in the 2 tables'c5' must appears because have different house in the 2 tables'c6' must appears because have different service in the 2 tables'c7' must appears because have different office and house in the 2 tables'c8' must appears because have different office and service in the 2 tables'c9' must appears because the records have different house and service in the 2 tables'c10' must appears because the records have different office house and service in the 2 tables'c11' doesnt appear because the records in the 2 tables are exactly equalsexamplepartner_info my_company_infogeneral_id general_id c2 nullnull c3partner_info my_company_infogeneral_id office_partner general_id office_co c4 usa c4 canadapartner_info my_company_infogeneral_id house_partner general_id house_co c5 toronto c5 otawapartner_info my_company_infogeneral_id service_partner general_id service_co c6 buy c6 sell partner_info my_company_infogeneral_id office house general_id office_ co house_co c7 canada toronto c7 usa nyand etc......ill really appreciate your helpmany thanks in advanced |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-13 : 09:44:15
|
[code]select p.general_id,p.[office_partner],p.[house_partner],p.[service_partner],m.[office_co],m.[house_co],m.[service_co]from partner_info pfull outer join my_company_info mon m.general_id = p.general_idwhere isnull(m.office_co,'') <> isnull(p.office_partner,'')or isnull(m.office_co,'') <> isnull(p.office_partner,'')or isnull(m.house_co,'') <> isnull(p.house_partner,'')or isnull(m.service_co,'') <> isnull(p.service_partner,'')[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sebastian11c
Posting Yak Master
129 Posts |
Posted - 2012-01-13 : 09:54:48
|
awesome, thanks visakh16 for helpping me at all timesbut im still having a problem with this query, because the record with general_id = 'c1' appear in the result, and i dont need it, becasuse the 2 record in the 2 tables are exactly equals,,could u give me an extra hand with thatthanks a lot again and have a nice day |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-13 : 10:03:32
|
that because you've multiple records for c1. in such cases, how do you determine which row needs to be compared to which row? is there some unique id field in partner_info table?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sebastian11c
Posting Yak Master
129 Posts |
Posted - 2012-01-13 : 10:42:57
|
there isnt some unique id in partner_info table, any idea to solve this,, anyone it doesnt matter wich one, ill folloe uthanks again |
 |
|
sebastian11c
Posting Yak Master
129 Posts |
Posted - 2012-01-14 : 08:14:49
|
hi select p.general_id,p.[office_partner],p.[house_partner],p.[service_partner], m.general_id, m.co_id,m.[office_co],m.[house_co],m.[service_co]from partner_info pfull outer join my_company_info mon m.general_id = p.general_idwhere isnull(m.office_co,'') <> isnull(p.office_partner,'')or isnull(m.office_co,'') <> isnull(p.office_partner,'')or isnull(m.house_co,'') <> isnull(p.house_partner,'')or isnull(m.service_co,'') <> isnull(p.service_partner,'')ill get all the mistakes that my_company madeexamplegeneral_i office_partner house_partner service_partner general_id co_id office_co house_co service_coc10 usa miami buy c10 10 canada toronto sellnow i want to fix all the mistakes how could i do? to fix all te mistakes ?the correct info always is the info into the table partner_info so i want to do something like this with all the mistakesexample with record c10update my_company_infoset office_co =office_partner, house_co=house_partner, service_co= service_partnerwhere co_id=10i want to do something like that but with the all mistakes,, so i dont know may be with a loop , im not sure,, could you give me some advice or examplethanks a lot again |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-14 : 08:25:24
|
[code]update cset m.[office_co] = case when isnull(m.office_co,'') <> isnull(p.office_partner,'') then p.[office_partner] else m.[office_co] end,m.[house_co]= case when isnull(m.house_co,'') <> isnull(p.house_partner,'') then p.[house_partner] else m.[house_co] end,m.[service_co] = case when isnull(m.service_co,'') <> isnull(p.service_partner,'') then p.[service_partner] else m.[service_co] endfrom partner_info p inner join my_company_info mon m.general_id = p.general_idwhere isnull(m.office_co,'') <> isnull(p.office_partner,'')or isnull(m.house_co,'') <> isnull(p.house_partner,'')or isnull(m.service_co,'') <> isnull(p.service_partner,'')insert my_company_infoselect p.columns..from partner_info pwhere not exists (select 1 from my_company_info where general_id = p.general_id) [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
sebastian11c
Posting Yak Master
129 Posts |
Posted - 2012-01-16 : 10:09:33
|
that works greatthanks again visakh16thanks for helping me alwaysi have another problem i want to update a third table called "invoice"and this table doenst have a column called "general_id" , only have the columns [co_id] [int] NULL, [office_co] [varchar](50) NULL, [house_co] [nchar](10) NULL, [service_co [nchar](10) NULL,, so after executing your awesome query to get all the mistakesselect p.general_id,p.[office_partner],p.[house_partner],p.[service_partner], m.general_id, m.co_id,m.[office_co],m.[house_co],m.[service_co]from partner_info pfull outer join my_company_info mon m.general_id = p.general_idwhere isnull(m.office_co,'') <> isnull(p.office_partner,'')or isnull(m.office_co,'') <> isnull(p.office_partner,'')or isnull(m.house_co,'') <> isnull(p.house_partner,'')or isnull(m.service_co,'') <> isnull(p.service_partner,'')also i need to update table "invoice" example with record c10update invoiceset office_co =office_partner, house_co=house_partner, service_co= service_partnerwhere co_id=10i want to do something like that but with the all mistakes,, so i dont know may be with a loop , im not sure,, could you give me some advice or examplethanks a lot againhave a nice day |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-16 : 10:22:50
|
the update will just the same as before only difference being tablenames will change and also conditionupdate iset i.[office_co] = case when isnull(i.office_co,'') <> isnull(m.office_co,'') then m.office_co else i.[office_co] end,i.[house_co]= case when isnull(i.house_co,'') <> isnull(m.house_co,'') then m.house_co else i.[house_co] end,i.[service_co] = case when isnull(i.service_co,'') <> isnull(m.service_co,'') then m.service_co else i.[service_co] endfrom invoice i inner join my_company_info mon m.co_id = i.co_idwhere isnull(m.office_co,'') <> isnull(i.office_co,'')or isnull(m.house_co,'') <> isnull(i.house_co,'')or isnull(m.service_co,'') <> isnull(i.service_co,'') ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|