Solved

MySQL query to get group total using Group by With ROLLUP?

Posted on 2013-01-04
1
2,610 Views
Last Modified: 2013-01-05
Please help me with MySQL query, thanks.

Here is my sample table:

ID  Service Quantity
3          a             3
1          a             2
12        c           22
5          c           6
10        c            3
20        b            1
11        b            1
4          b            2


I would like to get the subtotal and total, the data is grouped by and sorted by service, also in each group, ID is sorted,  the result is as belowt:
ID Service Quantity
1        a         2
3        a         3
Subtotal :    5
            
4       b         2
11     b         1
20     b         1
Subtotal:    4
            
5       c        6
10     c        3
12     c      22
Subtotal:   31  

Total:         40
0
Comment
Question by:HemlockPrinters
1 Comment
 
LVL 26

Accepted Solution

by:
Chris Luttrell earned 500 total points
ID: 38746372
This is a way to do it in SQL Server :
CREATE TABLE sample (ID INT,  Service CHAR(1), Quantity INT);
INSERT INTO SAMPLE VALUES 
(3, 'a', 3),
(1, 'a', 2),
(12, 'c', 22),
(5, 'c', 6),
(10, 'c', 3),
(20, 'b', 1),
(11, 'b', 1),
(4, 'b', 2);

SELECT	ID, 
	CASE WHEN Service IS NULL THEN 'Total:'  
		WHEN ID IS NULL THEN 'Subtotal:' 
		ELSE Service END Service, 
	SUM(Quantity) Quantity
FROM sample
GROUP BY Service, ID
WITH ROLLUP

DROP TABLE sample

Open in new window

which yields these results:Rollup Results(sampe written and run on SQL 2008 so INSERT will need tweaked for SQL 2005, but the SELECT ... WITH ROLLUP will work in 2005)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question