我有一个表格格式如下:
-----------------------
| Red | Blue | Yellow |
-----------------------
| 23 | 34 | 343 |
-----------------------
我试图找到一种方法,我可以用以下格式选择结果:
------------------
| Type | Count |
------------------
| Red | 23 |
| Blue | 34 |
| Yellow | 343 |
------------------
我正在使用条件,因此结果中总会有一行。
请告诉我使用哪种方式来实现这个目标?
我试过谷歌,找到了枢轴选项,但它需要汇总功能,我不知道它将如何应用于我的问题。
答案 0 :(得分:2)
您需要UNPIVOT
操作:
with yourTable(Red, Blue, Yellow) as (
select 23, 34, 343 from dual
)
select *
from yourTable
unpivot ( count for Type in (Red as 'Red', Blue as 'Blue', Yellow as 'Yellow'))
Here您了解了PIVOT
/ UNPIVOT
的一些有用示例。
答案 1 :(得分:1)
你可以使用union
select 'Red' type, Red
from my_table
union
select 'Blue' type, Blue
from my_table
union
select 'Yellow' type, Yellow
from my_table