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 |
|
pnpsql
Posting Yak Master
246 Posts |
Posted - 2012-06-30 : 04:57:51
|
| i have a table like tabl_schedule id tenor instno emi intrest prin 1 6 1 1000 1000 0 1 6 2 1000 1000 0 1 6 3 1000 1000 0 1 6 4 1000 1000 0 1 6 5 1000 1000 0 1 6 6 4000 1000 3000 now i like to update tenor column with 3 and required that only instno remains 1,2,3 and other records will be deleted and the amount in emi and prin column is updated like below id tenor instno emi intrest prin 1 6 1 1000 1000 0 1 6 2 1000 1000 0 1 6 3 4000 1000 3000 pls helpchallenge everything |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-06-30 : 13:44:59
|
| Can you explain the rule that you are using? Seems like you kept the first two rows, and then then changed the instno of the last row to 3. Is that the rule? "Keep first two rows and change the isntno of the last row to 3" Or is it something else? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-01 : 00:58:58
|
| [code];with cteas(select id, tenor, instno, emi, intrest, prin from(select row_number() over (partition by id, tenor order by instno desc) as rn,*from tabl_schedule )twhere rn=1)update tset t.emi = c.emi,t.prin = c.prinfrom tabl_schedule tinner join cte con c.id = t.idand c.tenor = t.tenorwhere t.instno = 3and c.instno > 3delete from tabl_schedule where instno > 3[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|