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 |
vikashsql
Starting Member
6 Posts |
Posted - 2008-05-15 : 04:55:11
|
Hi !!I have to update DOB in table2 from the DOB in table1 for ROLL Id's and if any ROLLID has status both as U and A , then we should consider the record with status A.Table1 ROLL STATUS DOBROLL1 U XXXXROLL2 A XXXXROLL3 A XXXXROLL4 A XXXX ROLL4 U ZZZZROLL2 U YYYYTABLE2ROLL DOBROLL1 ROLL2 ROLL3 ROLL4 P.S: DATA in table1 is basically a result of select query for some ROLL ID's , i've just given as table1 to make it clear.PLease help, i was trying to use grroup by and having but it's throwing error |
|
soorajtnpki
Posting Yak Master
231 Posts |
Posted - 2008-05-15 : 05:48:31
|
hi try thisdeclare @tb1 table(roll varchar(20),status varchar(20),dob varchar(20))insert @tb1 select 'ROLL1', 'U','XXXX'union all select 'ROLL2', 'A','XXXX'union all select 'ROLL3', 'A', 'XXXX'union all select 'ROLL4', 'A','XXXX'union all select 'ROLL4', 'U', 'ZZZZ'union all select 'ROLL2' ,'U', 'YYYY'declare @tb2 table(roll varchar(20),dob varchar(20))insert @tb2 select 'ROLL1','null'union all select 'ROLL2','null'union all select 'ROLL3' ,'null'union all select 'ROLL4' ,'null'delete from @tb1 where status='u' and roll in(select min(roll) from @tb1 group by roll having count(*) > 1)UPDATE tSET t.dob=yt.dobFROM @tb1 ytINNER JOIN @tb2 tON t.roll=yt.rollselect * from @tb2ok tanx.... |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-05-15 : 05:57:36
|
[code]declare @tb1 table(roll varchar(20),status varchar(20),dob varchar(20))insert @tb1 select 'ROLL1', 'U','XXXX'union all select 'ROLL2', 'A','XXXX'union all select 'ROLL3', 'A', 'XXXX'union all select 'ROLL4', 'A','XXXX'union all select 'ROLL4', 'U', 'ZZZZ'union all select 'ROLL2' ,'U', 'YYYY'declare @tb2 table(roll varchar(20),dob varchar(20))insert @tb2 select 'ROLL1','null'union all select 'ROLL2','null'union all select 'ROLL3' ,'null'union all select 'ROLL4' ,'null'update t2set dob = t1.dobfrom @tb2 t2 join (select roll, min(status) as status from @tb1 group by roll ) t on t2.roll = t.rolljoin @tb1 t1 on t.roll = t1.roll and t.status = t1.statusselect * from @tb2[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
vikashsql
Starting Member
6 Posts |
Posted - 2008-05-15 : 10:03:03
|
Thanks Man!I think this will not be the apt solution here as i've more than 100 reocrds :(Though , i implemented it otherwise by populating required data in a temp table and updating using this data .Please see the code below :SELECT TABLE1.ROLL , COUNT(TABLE1.ROLL) AS COUNT, TABLE1.DOB, TABLE1.STATUSINTO #TEMPFROM TABLE1 ,TABLE2 where TABLE1.ROLL = TABLE2.ROLLGROUP BY TABLE1.ROLL,TABLE1.STATUS,TABLE1.DOBHAVING ((COUNT(TABLE1.ROLL) > 1 AND TABLE1.STATUS = 'A') OR COUNT(TABLE1.ROLL)= 1 )UPDATE TABLE2SET TABLE2.DOB = T.DOBFROM TABLE2, #temp T WHERE TABLE2.ROLL = T.ROLL |
 |
|
vikashsql
Starting Member
6 Posts |
Posted - 2008-05-15 : 10:11:34
|
quote: Originally posted by harsh_athalye
declare @tb1 table(roll varchar(20),status varchar(20),dob varchar(20))insert @tb1 select 'ROLL1', 'U','XXXX'union all select 'ROLL2', 'A','XXXX'union all select 'ROLL3', 'A', 'XXXX'union all select 'ROLL4', 'A','XXXX'union all select 'ROLL4', 'U', 'ZZZZ'union all select 'ROLL2' ,'U', 'YYYY'declare @tb2 table(roll varchar(20),dob varchar(20))insert @tb2 select 'ROLL1','null'union all select 'ROLL2','null'union all select 'ROLL3' ,'null'union all select 'ROLL4' ,'null'update t2set dob = t1.dobfrom @tb2 t2 join (select roll, min(status) as status from @tb1 group by roll ) t on t2.roll = t.rolljoin @tb1 t1 on t.roll = t1.roll and t.status = t1.statusselect * from @tb2 Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
Thanks Man!!I already got it , problem was that i was trying to it one go . |
 |
|
|
|
|
|
|