2015년 1월 22일 목요일

MySQL 피벗 테이블 | MySQL Pivot Table

SELECT * FROM `table`;
column1 column2 column3
A 10 2
B 2 1
C 3 1
D 4 1
A 15 1
B 7 2
D 8 1
A 3 2
SELECT column1, SUM(column2) AS Total FROM `table` GROUP BY column1;
column1 Total
A 28
B 9
C 3
D 12
SELECT column1, SUM(IF(column3=1, column2, 0)) AS Jan, SUM(IF(column3=2, column2, 0)) AS Feb FROM `table` GROUP BY column1;
column1 Jan Feb
A 15 13
B 2 7
C 3 0
D 12 0
SELECT column1, SUM(IF(column3=1, column2, 0)) AS Jan, SUM(IF(column3=2, column2, 0)) AS Feb, SUM(column2) AS Total FROM `table` GROUP BY column1 WITH ROLLUP;
column1 Jan Feb Total
A 15 13 28
B 2 7 9
C 3 0 3
D 12 0 12

Read More

댓글 없음:

댓글 쓰기