Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I have a dificulties with calculation in one table

The table is like this:

ItemNumber |ItemName |Date | Quantity|

+-------------+-----------+----------+----------+

| 1 | Sprite |1.1.2011| 100

| 2 | Sprite |1.5.2011| -10

| 3 | Sprite |1.9.2011| 100

| 4 | Sprite |9.9.2011| -50



I want to make a calculation like this:





ItemNumber |ItemName | Date |Quantity | Result |

+-------------+-----------+----------+---------|---------+

| 1 | Sprite |1.1.2011| 100 | 100 Rezult=Quantity

| 2 | Sprite |1.5.2011| -10 | 90 Rezult=100-10=90

| 3 | Sprite |1.9.2011| 100 |190 Rezult=90+100=190

| 4 | Sprite |9.9.2011| -50 |140 Rezult=190-50

.

.

.

What function or syntax i may use to make this calculation in SQL Server 2008!



Kind Regarts!
Posted

1 solution

Try this

SQL
SELECT  ItemNumber,
        ItemName,
        Date,
        Quantity,
        [Result] = (SELECT SUM(Quantity) FROM MyTable WHERE ItemNumber=a.ItemNumber AND ItemName=a.ItemName AND Date<=a.Date)
FROM    MyTable a
 
Share this answer
 
Comments
Atmir 30-Mar-11 9:17am    
thnx it done !
Kind Regards!
Costica U 30-Mar-11 9:57am    
Please mark it as accepted solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900