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 |
|
WoodHouse
Posting Yak Master
211 Posts |
Posted - 2009-07-30 : 05:01:20
|
| [code] hiI have table like this...A B C D E1 5 T 2 6 F 3 7 T 4 3 T 5 6 T In column D i want the total upto C column value is 'F' which means (5+6)=11 And How to update the values like E column(values based on A col)....Like this...A B C D E1 5 T 0 12 6 F 11 1:23 7 T 0 1:2:34 3 T 0 1:2:3:45 6 T 0 1:2:3:4:5Pls help me out.. [/code] |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-07-30 : 06:02:40
|
| [code]declare @tab table(A int, B int, C char(1), D int, E varchar(32))insert into @tab (a,b,c) select 1, 5, 'T' union all select 2, 6, 'F' union all select 3, 7, 'T' union all select 4, 3, 'T' union all select 5, 6, 'T' select * from @tabupdate tset t.d = s.valfrom @tab tinner join (select t.a,sum(c.b) as valfrom @tab t inner join @tab c on c.a <= t.awhere t.c ='F' group by t.a ) s on s.a = t. a;with cte(a,id )as(select top 1 a , CAST(a AS VARCHAR(801) ) from @tab union allselect t.a, cast(c.id as varchar(400)) + ':'+ cast(t.a as varchar(400))from @tab t inner join cte c on c.a+1 = t.a )update tset t.e = c.idfrom @tab tinner join cte c on c.a = t.aselect * from @tab[/code] |
 |
|
|
|
|
|
|
|