Thursday, February 16, 2012

**sum**

Hi
I've follwoing data in table1 in SQL server2000,and I want to get the
result as below:
table1 result
Id Qty
-- --
1 500 1 500
2 502 2 1002
3 400 3 1402
4 140 4 1542
how is it possible in select statement?
Any help would be thankful.Here you go
CREATE TABLE #Table1(id int PRIMARY KEY, Quantity int NOT NULL)
INSERT INTO #Table1 VALUES(1,500)
INSERT INTO #Table1 VALUES(2,502)
INSERT INTO #Table1 VALUES(3,400)
INSERT INTO #Table1 VALUES(4,140)
SELECT id,
(SELECT SUM(Quantity) FROM #Table1 WHERE id <= O.id) As RunningTotal
FROM #Table1 O
Roji. P. Thomas
Net Asset Management
http://toponewithties.blogspot.com
<R> wrote in message news:opsyn8fezjmw7tkz@.system109.parskhazar.net...
> Hi
> I've follwoing data in table1 in SQL server2000,and I want to get the
> result as below:
> table1 result
> Id Qty
> -- --
> 1 500 1 500
> 2 502 2 1002
> 3 400 3 1402
> 4 140 4 1542
>
> how is it possible in select statement?
> Any help would be thankful.|||I missed the ORDER BY
SELECT id,
(SELECT SUM(Quantity) FROM #Table1 WHERE id <= O.id) As RunningTotal
FROM #Table1 O
ORDER By ID
Roji. P. Thomas
Net Asset Management
http://toponewithties.blogspot.com
"Roji. P. Thomas" <thomasroji@.gmail.com> wrote in message
news:ecqyxfV0FHA.3892@.TK2MSFTNGP12.phx.gbl...
> Here you go
>
> CREATE TABLE #Table1(id int PRIMARY KEY, Quantity int NOT NULL)
> INSERT INTO #Table1 VALUES(1,500)
> INSERT INTO #Table1 VALUES(2,502)
> INSERT INTO #Table1 VALUES(3,400)
> INSERT INTO #Table1 VALUES(4,140)
> SELECT id,
> (SELECT SUM(Quantity) FROM #Table1 WHERE id <= O.id) As RunningTotal
> FROM #Table1 O
>
> --
> Roji. P. Thomas
> Net Asset Management
> http://toponewithties.blogspot.com
>
> <R> wrote in message news:opsyn8fezjmw7tkz@.system109.parskhazar.net...
>

No comments:

Post a Comment