Home All Groups Group Topic Archive Search About
Author
12 Aug 2006 6:05 AM
M
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/

Author
12 Aug 2006 7:13 AM
David Portas
M wrote:
Show quote
> 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/

In the absence of any other information I'll assume that f2 is the key
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
--
Author
12 Aug 2006 7:18 AM
Omnibuzz
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

--
-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/



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/
>

AddThis Social Bookmark Button