Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi I used the following in my query select id = case when name = (select min(name) from t t2 where t.id = t2.id) then id else null end ,name, Qtyfrom torder by id, nameand its working fine and giving me an output as followsGroupID UserName Qty ------- -------- ------- 100 Alan 100 Bob 100 Craig 100200 Daniel 200 Mike 300Now I want My Out put as follows:GroupID UserName Qty ------- -------- ------- 100 Alan 100 Bob 100 Craig 100Subtotal 300200 Daniel 200 Mike 300Subtotal 500Grand Total 800Can Anybody HelpThanks
Kristen
Test
22859 Posts
Posted - 2004-10-16 : 05:00:35
select id = case when name = (select min(name) from t t2 where t.id = t2.id) then id else null end ,name, SUM(Qty)from tGROUP BY case when name = (select min(name) from t t2 where t.id = t2.id) then id else null end, nameWITH ROLLUPorder by id, nameKristen
skchaman
Starting Member
6 Posts
Posted - 2004-10-16 : 08:21:53
Dear Kristen,Thanks for the promp posting of reply, but it gives error while running in query analyser as follows;Cannot use an aggregate or a subquery in an expression used for the group by list of a group by clause.Please sugestThanks
Kristen
Test
22859 Posts
Posted - 2004-10-16 : 10:21:19
I suppose you can't have the subselect in there with the GROUP BY. Convert that a JOIN instead.Here's an example
USE NorthwindGOSELECT CustomerID, ProductID, SUM(Quantity)FROM dbo.Orders O JOIN dbo.[Order Details] OD ON O.OrderID = OD.OrderIDGROUP BY CustomerID, ProductID WITH ROLLUPORDER BY CustomerID, ProductID