Hi,
Regarding the usage of GROUPING SETS, please refer to the following example:
Please refer to:
CREATE TABLE inventory (
warehouse VARCHAR(255),
product VARCHAR(255) NOT NULL,
model VARCHAR(50) NOT NULL,
quantity INT,
PRIMARY KEY (warehouse,product,model)
);
INSERT INTO inventory(warehouse, product, model, quantity)
VALUES('San Jose', 'iPhone','6s',100);
INSERT INTO inventory(warehouse, product, model, quantity)
VALUES('San Fransisco', 'iPhone','6s',50);
INSERT INTO inventory(warehouse, product, model, quantity)
VALUES('San Jose','iPhone','7',50);
INSERT INTO inventory(warehouse, product, model, quantity)
VALUES('San Fransisco', 'iPhone','7',10);
INSERT INTO inventory(warehouse, product, model, quantity)
VALUES('San Jose','iPhone','X',150);
INSERT INTO inventory(warehouse, product, model, quantity)
VALUES('San Fransisco', 'iPhone','X',200);
INSERT INTO inventory(warehouse, product, model, quantity)
VALUES('San Jose','Samsung','Galaxy S',200);
INSERT INTO inventory(warehouse, product, model, quantity)
VALUES('San Fransisco','Samsung','Galaxy S',200);
INSERT INTO inventory(warehouse, product, model, quantity)
VALUES('San Fransisco','Samsung','Note 8',100);
INSERT INTO inventory(warehouse, product, model, quantity)
VALUES('San Jose','Samsung','Note 8',150);
SELECT * FROM inventory;
SELECT warehouse,product, SUM (quantity) qty
FROM inventory
GROUP BY warehouse,product;
SELECT warehouse,SUM (quantity) qty
FROM inventory
GROUP BY warehouse;
SELECT product,SUM (quantity) qty
FROM inventory
GROUP BY product;
SELECT SUM(quantity) qty
FROM inventory;
SELECT warehouse,product, SUM (quantity) qty
FROM inventory
GROUP BY
GROUPING SETS((warehouse,product),(warehouse),(product),());
For more details, please refer to:
SQL GROUPING SETS
If you have any question, please feel free to let me know.
Regards
Echo
If the answer is helpful, please click "Accept Answer" and upvote it.