-1

Here is My query :

SELECT Name,
       Quantity,
       Price,
       (Quantity*Price)[Total Price] 
  FROM Menu,
       Items_per_Table,
       [735294] 
 WHERE Menu.Item_id = Items_per_Table.Item_id 
       AND [735294].OrderNo = Items_per_Table.OrderNo 
       AND Bill_Generated = 'True'

It returns :

Name                  Quantity   Price   Total Price
Aerated Drinks             1     30.00   30.00
Fresh Lime Soda/Water      3     60.00   180.00

Here I want the sum of total prices like this : 30+180 = 210 in another column. How can I perform that ?

It Should Return :

Name                  Quantity   Price   Total Price    All Over Price
Aerated Drinks             1     30.00   30.00          30
Fresh Lime Soda/Water      3     60.00   180.00         210
Rocky Balboa
  • 784
  • 10
  • 25

2 Answers2

0

For SQL Server 2012, this should do what you need;

SELECT Name,
       Quantity,
       Price,
       (Quantity*Price) [Total Price],
       SUM(Quantity*Price) OVER (ORDER BY menu.item_id) [All Over Price] 
  FROM Menu,
       Items_per_Table,
       [735294] 
 WHERE Menu.Item_id = Items_per_Table.Item_id 
       AND [735294].OrderNo = Items_per_Table.OrderNo 
       AND Bill_Generated = 'True'
ORDER BY menu.item_id

SUM(...) OVER (ORDER BY...) does the running sum in the order you give it. Since you have no order in your question, I arbitrarily chose menu.item_id to order by.

EDIT: Forgot to add the SQLfiddle to test with.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

Try using Common Table Expressions,(If you are using SQL < SQl 2012)

;with cteabc
as (
SELECT Name,
       Quantity,
       Price,
       (Quantity*Price)[Total Price] 
  FROM Menu,
       Items_per_Table,
       [735294] 
 WHERE Menu.Item_id = Items_per_Table.Item_id 
       AND [735294].OrderNo = Items_per_Table.OrderNo 
       AND Bill_Generated = 'True'
    )
select
    t1.Name,
    t1.Quantity,
    t1.Price,
    rt.runningTotal
from cteabc t1
 cross apply (select sum([Total Price]) as runningTotal
                from cteabc t2
                where  t2.rn<= t1.rn
            ) as rt
order by t1.Name

SQL Fiddle

Prahalad Gaggar
  • 11,389
  • 16
  • 53
  • 71