Question : T-SQL: calculate totals in SELECT statement witout GROUP BY

I have a simple select statement and would like to see the totals without using GROUP BY. Do I use COMPUTE, or CUBE or how can I get the output as described below?

This is the "order" table... it lists many products, many "producs" have many "order" lines... I want to see all the order lines, and a total column when the next product shows...

i.e.

product 1    2    $30
product 1    4    $60
            TOTAL: $90
product 2    2    $10
product 2    4    $20
            TOTAL: $30
product 3    1    $20
product 3    1    $20
            TOTAL: $40
Code Snippet:
1:
2:
3:
4:
5:
6:
SELECT 
    name, qty, price 
FROM 
    order 
ORDER BY 
    name

Answer : T-SQL: calculate totals in SELECT statement witout GROUP BY

Without GROUP BY, its not possible. You can try using ROLLUP as acperkins suggested.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
declare @table as table(ProductName varchar(20),Quant int,Val int)
insert into @table values('Product 1',2,30)
insert into @table values('Product 1',4,60)
insert into @table values('Product 2',2,10)
insert into @table values('Product 2',4,20)
insert into @table values('Product 3',1,20)
insert into @table values('Product 3',1,20)
select ProductName,isnull(convert(varchar,Quant),'Total:') Quant,Val 
  from (select ProductName,Quant,sum(Val) as Val from @table group by ProductName,Quant with rollup) as t1
 where ProductName is not null
/*
Product 1	2	30
Product 1	4	60
Product 1	Total:	90
Product 2	2	10
Product 2	4	20
Product 2	Total:	30
Product 3	1	40
Product 3	Total:	40
*/
Random Solutions  
 
programming4us programming4us