Wednesday, June 27, 2007

Running total sql-server group by

OrderID Oredrty OrderAmt OrderDate
1 SO 10 ########
2 SR 36 ########
3 SO 25 ########
4 SR 21 ########
5 SO 10 ########
6 CO 100 ########
7 CO 20 ########
8 SO 100 ########

SELECT a.OrderID, a.OrderAmt,A.Oredrty, SUM(b.OrderAmt)RunningTotal FROM orders a
CROSS JOIN orders b WHERE (b.OrderID <= a.OrderID AND b.Oredrty = a.Oredrty)
GROUP BY A.Oredrty,a.OrderID,a.OrderAmt
ORDER BY A.Oredrty,a.OrderID,a.OrderAmt

www.svdeals.com super value deals

No comments: