我有一张表,我想从中将行值显示为列值。
据我所知,PIVOT功能用于此。
原始表结构如下https://ibb.co/MVdsD01
,所需的表应类似于https://ibb.co/Vt704WF
因此,从以上两幅图像中,您可以发现类别,颜色和百分比列的值希望按行显示为行值。
答案 0 :(得分:0)
您不需要枢轴,只需使用CASE
SELECT line,
MAX(CASE WHEN category = 'UT' THEN duration END) AS ut_min,
MAX(CASE WHEN category = 'UT' THEN percentage END) AS ut_percentage,
MAX(CASE WHEN category = 'UT' THEN color END) AS ut_color,
MAX(CASE WHEN category = 'AOOT' THEN duration END) AS aoot_min,
MAX(CASE WHEN category = 'AOOT' THEN percentage END) AS aoot_percentage,
MAX(CASE WHEN category = 'AOOT' THEN color END) AS aoot_color
--, the rest of your columns...
FROM table_name
GROUP BY line
ORDER BY line;
答案 1 :(得分:0)
为什么不像下面这样进行条件聚合
book