Author |
Topic |
sachingovekar
Posting Yak Master
101 Posts |
Posted - 2011-11-02 : 04:11:48
|
Hi,I want to update table 1 from table 2. Below is the out put which I need.SELECT TOP 5 * FROM SG89411.SW_INSTALLS_11022011create table #table1(product varchar(100), match int)insert into #table1 values ('adobe',null)insert into #table1 values ('adobe',null)insert into #table1 values ('citrix',null)insert into #table1 values ('citrix',null)insert into #table1 values ('citrix',null)create table #table2(product varchar(100), idno int)insert into #table2 values ('adobe',1)insert into #table2 values ('adobe',2)insert into #table2 values ('adobe',3)insert into #table2 values ('citrix',1)insert into #table2 values ('citrix',3)update #table1set match = t2.idnofrom #table1 t1 join #table2 t2on t1.product = t2.productneed out put as:product matchadobe 1adobe 2citrix 1citrix 3citrix nullRegards,Sachin |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-11-02 : 04:22:55
|
[code]update t1set match = t2.idnofrom ( select *, rn = row_number() over (partition by product order by product) from #table1 ) t1 inner join ( select *, rn = row_number() over (partition by product order by idno) from #table2 ) t2 on t1.product = t2.product and t1.rn = t2.rn[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
sachingovekar
Posting Yak Master
101 Posts |
Posted - 2011-11-02 : 04:48:36
|
using the above update statement, i am getting below output.product matchadobe 1adobe 2citrix 1citrix 2citrix 1I should getproduct matchadobe 1adobe 2citrix 1citrix 3citrix null |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-11-02 : 04:53:26
|
identical query, no changes at all ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
sachingovekar
Posting Yak Master
101 Posts |
Posted - 2011-11-02 : 05:20:15
|
Thank you it worked now. |
 |
|
sachingovekar
Posting Yak Master
101 Posts |
Posted - 2011-11-03 : 00:23:12
|
I am looking for a scenario like below:TABLE 1vendor product matchadobe reader adobe reader microsoft word microsoft word IBM word crystal reader TABLE2vendor product entid statusadobe reader 1 adobe reader 2 adobe reader 3 microsoft word 4 IBM word 5 unknown reader 6 1. first match vendor columns of both tables and update both tables: populate entid of table 2 in match column of table1 and status of table2 will be populated as used which means that row is used and should not be used further.2. then second match should be on product final output of both tables:TABLE 1vendor product matchadobe reader 1adobe reader 2microsoft word 4microsoft word nullIBM word 5crystal reader 3TABLE 2vendor product entid statusadobe reader 1 usedadobe reader 2 usedadobe reader 3 usedmicrosoft word 4 usedIBM word 5 usedunknown reader 6 null |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-11-03 : 00:27:09
|
[code]update t1set match = t2.idnofrom ( select *, rn = row_number() over (partition by vendor, product order by product) from #table1 ) t1 inner join ( select *, rn = row_number() over (partition by vendor, product order by idno) from #table2 ) t2 on t1.product = t2.product and t1.vendor = t2.vendor and t1.rn = t2.rn[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
sachingovekar
Posting Yak Master
101 Posts |
Posted - 2011-11-03 : 00:39:46
|
my first update should be like below-- first updateupdate table1 join table2 on vendor = vendor-- second updateupdate table1 join table2 on product = product where table 2 rows are not used in first updatethe objective here is update unique id of table 2 into table1 using matching criteria and simultaneosly mark the row of table2 as "used" so that it is not used for further updates (means not using unique entid again). |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-11-03 : 00:43:23
|
can you post some sample data and the result after 1st and 2nd update ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
sachingovekar
Posting Yak Master
101 Posts |
Posted - 2011-11-03 : 00:54:15
|
BEFORETABLE1 vendor product matchadobe reader NULLadobe reader NULLmicrosoft word NULLmicrosoft word NULLIBM word NULLcrystal reader NULLTABLE2 vendor product entid statusadobe reader 1 NULLadobe reader 2 NULLadobe reader 3 NULLmicrosoft word 4 NULLIBM word 5 NULLunknown reader 6 NULLAFTER UPDATE--------------------------------------TABLE1 vendor product matchadobe reader 1adobe reader 2microsoft word 4microsoft word nullIBM word 5crystal reader 3TABLE2 vendor product entid statusadobe reader 1 usedadobe reader 2 usedadobe reader 3 usedmicrosoft word 4 usedIBM word 5 usedunknown reader 6 null |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-11-03 : 02:44:45
|
[code]declare @used table( entid int)-- UPDATE 1 by vendorupdate t1set match = t2.entidoutput t2.entidinto @used (entid)from ( select *, rn = row_number() over (partition by vendor order by vendor) from #table1 ) t1 inner join ( select *, rn = row_number() over (partition by vendor order by entid) from #table2 where status is null ) t2 on t1.vendor = t2.vendor and t1.rn = t2.rnupdate t2set status = 'used'from #table2 t2 inner join @used u on t2.entid = u.entidwhere t2.status is null-- UPDATE 2 by productupdate t1set match = t2.entidoutput t2.entidinto @used (entid)from ( select *, rn = row_number() over (partition by product order by product) from #table1 where match is null ) t1 inner join ( select *, rn = row_number() over (partition by product order by entid) from #table2 where status is null ) t2 on t1.product = t2.product and t1.rn = t2.rnupdate t2set status = 'used'from #table2 t2 inner join @used u on t2.entid = u.entidwhere t2.status is null[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|