Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Create A Running Total In A Query
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01327963
Message ID:
01328107
Views:
18
>>>I don't have Nortwind here. But this is not the running total, this is just a sum of all order details for each Order record. What exactly did you want to produce? Running total usually involves dates.
>>
>>Yes, I want a running total. I'm not too sure how to do that.
>
>What is your table structure and what do you mean by running total?
>
>Check also http://www.developerfusion.co.uk/show/1734/


This is what I'm looking for!!
SELECT	a.ProductId,
        a.UnitPrice,
	a.Quantity,
    SUM(a.UnitPrice * a.Quantity) AS RunningTotal
    FROM [Order Details] a 
    CROSS JOIN [Order Details] b
    WHERE (b.ProductId <= a.ProductId) 
    GROUP BY a.ProductId, a.UnitPrice, a.Quantity
    ORDER BY a.ProductId, a.UnitPrice, a.Quantity
Which gives me:
ProductId   UnitPrice             Quantity RunningTotal
----------- --------------------- -------- ---------------------
1           14.40                 10       5472.00
1           14.40                 12       6566.40
1           14.40                 15       24624.00
1           14.40                 18       9849.60
2           15.20                 7        8724.80
2           15.20                 10       12464.00
2           15.20                 20       49856.00
2           15.20                 24       29913.60
2           15.20                 25       62320.00
2           19.00                 5        15580.00
2           19.00                 8        12464.00
3           8.00                  20       15040.00
3           8.00                  30       22560.00
3           10.00                 25       23500.00
3           10.00                 30       28200.00
3           10.00                 49       46060.00
3           10.00                 60       56400.00
4           17.60                 10       20064.00
4           17.60                 16       32102.40
4           17.60                 18       36115.20
4           22.00                 21       52668.00
4           22.00                 25       188100.00
4           22.00                 30       75240.00
4           22.00                 50       376200.00
Thanks for pointing me in the right direction!
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform