SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 update help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

pnpsql
Posting Yak Master

India
241 Posts

Posted - 06/30/2012 :  04:57:51  Show Profile  Reply with Quote
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 help

challenge everything

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 06/30/2012 :  13:44:59  Show Profile  Reply with Quote
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?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47069 Posts

Posted - 07/01/2012 :  00:58:58  Show Profile  Reply with Quote

;with cte
as
(
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 
)t
where rn=1
)

update t
set t.emi = c.emi,
t.prin = c.prin
from tabl_schedule  t
inner join cte c
on c.id = t.id
and c.tenor = t.tenor
where t.instno = 3
and c.instno > 3


delete from tabl_schedule  where instno > 3


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000