我有一个包含以下结构的表: 列名称是指标(MT_NAME,MT_ADDRESS,MT_PHONE等),行是来自该发生的指标的值/得分(0,6,8,9,10)。 像这样:
+---------+------------+----------+
| MT_NAME | MT_ADDRESS | MT_PHONE |
+---------+------------+----------+
| 0 | 9 | 6 |
| 6 | 10 | 0 |
| 8 | 10 | 0 |
+---------+------------+----------+
我想构建一个包含3列的视图:一列具有表格列的名称,一列具有该列的不同值,另一列具有该列中出现的值的频率。 像这样:
+------------+----------+-------+
| LEGEND | CATEGORY | VALUE |
+------------+----------+-------+
| MT_NAME | 0 | 1 |
| MT_NAME | 6 | 1 |
| MT_NAME | 8 | 1 |
| MT_ADDRESS | 9 | 1 |
| MT_ADDRESS | 10 | 2 |
| MT_PHONE | 0 | 2 |
| MT_PHONE | 6 | 1 |
+------------+----------+-------+
任何人都可以帮忙吗?
答案 0 :(得分:0)
您可以使用union all
:
select 'MT_NAME' as legend, mt_name as category, count(*) as frequency
from t
group by mt_name
union all
select 'MT_ADDRESS' as legend, mt_address as category, count(*) as frequency
from t
group by mt_name
union all
select 'MT_PHONE' as legend, mt_phone as category, count(*) as frequency
from t
group by mt_name;