1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| use world;
-- 예시 데이터 CREATE TABLE sales ( product VARCHAR(50), category VARCHAR(50), amount DECIMAL(10, 2) );
INSERT INTO sales (product, category, amount) VALUES ('ProductA', 'Category1', 100.00), ('ProductB', 'Category1', 150.00), ('ProductA', 'Category2', 200.00), ('ProductB', 'Category2', 250.00);
-- 피벗 SELECT product, SUM(CASE WHEN category = 'Category1' THEN amount END) AS Category1, SUM(CASE WHEN category = 'Category2' THEN amount END) AS Category2 FROM sales GROUP BY product;
|