Solved

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

Posted on 2013-01-04
1
2,571 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
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 …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now