表X中的原始数据
Date # Method # Count #
======================================
2012-08-24 # A # 44 #
2012-08-24 # B # 5 #
2012-08-24 # D # 1 #
* p / s共有8种方法,分别是a,b,c,d,e,f,g,h
我想要显示的内容如下:
Date # A # B # C # D # E # F # G # H #
2012-08-24 # 44 # 5 # 0 # 1 # 0 # 0 # 0 # 0 #
答案 0 :(得分:0)
试试这个:
SELECT A.Date,
SUM(CASE WHEN A.Methos = 'A' THEN A.Count ELSE 0 END) AS 'A',
SUM(CASE WHEN A.Methos = 'B' THEN A.Count ELSE 0 END) AS 'B',
SUM(CASE WHEN A.Methos = 'C' THEN A.Count ELSE 0 END) AS 'C',
SUM(CASE WHEN A.Methos = 'D' THEN A.Count ELSE 0 END) AS 'D',
SUM(CASE WHEN A.Methos = 'E' THEN A.Count ELSE 0 END) AS 'E',
SUM(CASE WHEN A.Methos = 'F' THEN A.Count ELSE 0 END) AS 'F',
SUM(CASE WHEN A.Methos = 'G' THEN A.Count ELSE 0 END) AS 'G',
SUM(CASE WHEN A.Methos = 'H' THEN A.Count ELSE 0 END) AS 'H'
FROM tableA A
GROUP BY A.Date
答案 1 :(得分:0)
您可以选择使用IF
SELECT
A.Date,
IF(A.Methos = 'A',A.Count,0) AS 'A',
IF(A.Methos = 'B',A.Count,0) AS 'B',
IF(A.Methos = 'C',A.Count,0) AS 'C',
IF(A.Methos = 'D',A.Count,0) AS 'D',
IF(A.Methos = 'E',A.Count,0) AS 'E',
IF(A.Methos = 'F',A.Count,0) AS 'F',
IF(A.Methos = 'G',A.Count,0) AS 'G',
IF(A.Methos = 'H',A.Count,0) AS 'H',
FROM tableA A
GROUP BY A.Date