Click here to Skip to main content
14,982,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

Below is my table
and data

SQL
CREATE TABLE car_uploads(
  location int(3),          -- Location ID
  item varchar(12),         -- Part ID
  quantity int(4)           -- Qty uploaded
);

INSERT INTO car_uploads (location, item, quantity) VALUES(001,'RED CAR',1);
INSERT INTO car_uploads (location, item, quantity) VALUES(002,'RED CAR',3);
INSERT INTO car_uploads (location, item, quantity) VALUES(003,'BLUE CAR',5);
INSERT INTO car_uploads (location, item, quantity) VALUES(002,'BLUE CAR',2);
INSERT INTO car_uploads (location, item, quantity) VALUES(001,'RED CAR',2);
INSERT INTO car_uploads (location, item, quantity) VALUES(002,'RED CAR',5);


below is the code where i am getting the sum of quantity column

SQL
SELECT item,
       SUM(CASE WHEN location = 1 THEN quantity ELSE 0 END) location_001,
       SUM(CASE WHEN location = 2 THEN quantity ELSE 0 END) location_002,
       SUM(CASE WHEN location = 3 THEN quantity ELSE 0 END) location_003,
       SUM(quantity) total,       
  FROM car_uploads
 GROUP BY item,total

now i want the sum of the total (i.e. sum(quantity) as grandtotal.

please tell how to get that..
Posted
Updated 21-Sep-14 22:35pm
v2

Use Group by with rollup

here is example on sqlfiddel

http://sqlfiddle.com/#!3/ae5db/6[^]

This is for you .. Cheers...!!

SQL
SELECT item,
SUM(CASE WHEN location = 1 THEN quantity ELSE 0 END) as location_001,
SUM(CASE WHEN location = 2 THEN quantity ELSE 0 END) as location_002,
SUM(CASE WHEN location = 3 THEN quantity ELSE 0 END) as location_003,
SUM(quantity) total
  FROM car_uploads

GROUP BY item WITH ROLLUP
   
v2
Comments
Black_Rose 22-Sep-14 4:41am
   
Thanks...:)
Gihan Liyanage 22-Sep-14 4:46am
   
Your welcome, and see the build query for you ..
You can use COMPUTE after your statement eg COMPUTE(SUM(Quantity)) after your group by line

You can also nest this inside another query eg

SQL
Select sum(Quantity) from(SELECT item,
       SUM(CASE WHEN location = 1 THEN quantity ELSE 0 END) location_001,
       SUM(CASE WHEN location = 2 THEN quantity ELSE 0 END) location_002,
       SUM(CASE WHEN location = 3 THEN quantity ELSE 0 END) location_003,
       SUM(quantity) total,       
  FROM car_uploads
 GROUP BY item,total) X
   
Comments
Black_Rose 22-Sep-14 4:46am
   
It's giving below error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM car_uploads GROUP BY item,total) X' at line 6: Select sum(Quantity) from(SELECT item, SUM(CASE WHEN location = 1 THEN quantity ELSE 0 END) location_001, SUM(CASE WHEN location = 2 THEN quantity ELSE 0 END) location_002, SUM(CASE WHEN location = 3 THEN quantity ELSE 0 END) location_003, SUM(quantity) total, FROM car_uploads GROUP BY item,total) X
Mycroft Holmes 22-Sep-14 4:50am
   
Well next time you might consider telling us you are using MySQL - the above answer is for MSSQL.
Black_Rose 22-Sep-14 5:51am
   
ya..i am sry for that..i totally forgot..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900