|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
**sum of field**I want to add the value of f3 field step by step in each row like below: f2 f3 --- ----- b 4 c 5 d 3 result: f2 f3 --- ---- b 4 c 9 (4+5) d 12 (4+5+3) I would be thankful if you send me a reply as soon as possible. M wrote:
Show quote > hi In the absence of any other information I'll assume that f2 is the key> > I want to add the value of f3 field step by step in each row like below: > > f2 f3 > --- ----- > b 4 > c 5 > d 3 > > result: > > f2 f3 > --- ---- > b 4 > c 9 (4+5) > d 12 (4+5+3) > > I would be thankful if you send me a reply as soon as possible. > > > -- > Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ of this table. If I'm wrong then you may get a different result from what you expect. SELECT t1.f2, SUM(t2.f3) AS f3 FROM tbl AS t1 JOIN tbl AS t2 ON t1.f2 >= t2.f2 GROUP BY t1.f2 ; -- David Portas, SQL Server MVP Whenever possible please post enough code to reproduce your problem. Including CREATE TABLE and INSERT statements usually helps. State what version of SQL Server you are using and specify the content of any error messages. SQL Server Books Online: http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx -- create table #temp (f2 char(1), f3 int)
insert into #temp values('b', 4) insert into #temp values('c', 5) insert into #temp values('d', 3) select a.f2, sum(b.f3) from #temp a,#temp b where a.f2>=b.f2 group by a.f2 Show quote "M" wrote: > > hi > > I want to add the value of f3 field step by step in each row like below: > > f2 f3 > --- ----- > b 4 > c 5 > d 3 > > result: > > f2 f3 > --- ---- > b 4 > c 9 (4+5) > d 12 (4+5+3) > > I would be thankful if you send me a reply as soon as possible. > > > -- > Using Opera's revolutionary e-mail client: http://www.opera.com/mail/ > |
|||||||||||||||||||||||