Solved

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

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

10 Experts available now in Live!

Get 1:1 Help Now