如果GROUPBY子句中至少存在一个值,如何为每种颜色创建单独的列?是否每个颜色列都需要一个带有GROUPBY子句的单独select语句。提前谢谢。
例如
SELECT
COUNT(ColorID) AS IcdCodecount,
Colorname
-- Blue -> if at least one value exists in 1, 10, 12
-- Red -> if at least one value in 0, 3, 4, 15
-- White -> if at least one value in 11, 12, 13, 14
-- Yellow -> if at least one value in 20, 21, 22, 23
FROM
TestTable
WHERE ColorID IN (
1, 10, 12, -- blue
0, 3, 4, 15, -- Red
11, 12, 13, 14, -- White
20, 21, 22, 23, -- yellow)
GROUP BY Colorname
2条答案
按热度按时间qv7cva1a1#
可以使用条件聚合:
4szc88ey2#
尝试使用数据透视表。oracle的实现描述如下:
https://www.oracletutorial.com/oracle-basics/oracle-pivot/