oracle 将文本值行转为列
oracle transposing text value rows to columns
我有一个 table 在 Oracle 中设计如下
+-----------+------+-------+
| ID | TYPE | VALUE |
+-----------+------+-------+
| 1 | A | 1 |
| 1 | B | 2 |
| 1 | C | 3 |
| 2 | A | 4 |
| 2 | B | 5 |
| 2 | C | 6 |
+-----------+------+-------+
我需要像这样转置这个table
+-----------+------+-------+-----+
| ID | A | B | C |
+-----------+------+-------+-----+
| 1 | 1 | 2 | 3 |
| 2 | 4 | 5 | 6 |
+-----------+------+-------+-----+
下面是我写的 sql 使用 oracle pivot 函数将这些行转置为列。
select * from
(
select ID, TYPE, VALUE from table where TYPE in ('A','B','C')
)
PIVOT (
max(value)
for TYPE in (1 column_a, 2 column_b, 3 column_c)
)
所以这些是我的问题
- 这个 returns 结果是 returns 空值,即使它不为空。它返回 NULL 是因为我正在使用带有字符串值的聚合函数吗?
- 此 SQL 没有将值映射到类型。所以基本上值总是需要绑定到类型列。这对 PIVOT 功能可行吗?
我也在考虑通过将 INSERT 与 SELECT 一起使用来重新创建 table。请告知使用 PIVOT 是否可行。
我发现使用条件聚合更容易:
select id,
max(case when type = 'A' then value end) as a,
max(case when type = 'B' then value end) as b,
max(case when type = 'C' then value end) as c
from t
group by id;
您可以使用 create table as
将结果插入 table。这也应该适用于数据透视查询。
1,2,3 为枢轴应该是 'A','B','C'
with CTE (ID,Type,Value) as (
SELECT 1, 'A',1 from dual union all
SELECT 1, 'B',2 from dual union all
SELECT 1, 'C',3 from dual union all
SELECT 2, 'A',4 from dual union all
SELECT 2, 'B',5 from dual union all
SELECT 2, 'C',6 from dual)
SELECT *
FROM (SELECT ID, TYPE, VALUE FROM cte WHERE TYPE in ('A','B','C'))
PIVOT (sum(value)
for TYPE in ('A' "A",'B' "B",'C' "C")) --Type has values of A,B,C so you have
--to pivot on A,B,C.. 1,2,3 are the values.
它返回 NULL 是因为我正在使用带有字符串值的聚合函数吗?
不,因为你的重点是错误的。以类型为中心而不是类型值
此 SQL 没有将值映射到类型。 *在我看来只是因为你在错误的事情上旋转......
所以基本上值总是需要绑定到类型列。这对 PIVOT 功能可行吗? 是的,在我看来你差了 3 个字符
工作示例的IMG:
我有一个 table 在 Oracle 中设计如下
+-----------+------+-------+
| ID | TYPE | VALUE |
+-----------+------+-------+
| 1 | A | 1 |
| 1 | B | 2 |
| 1 | C | 3 |
| 2 | A | 4 |
| 2 | B | 5 |
| 2 | C | 6 |
+-----------+------+-------+
我需要像这样转置这个table
+-----------+------+-------+-----+
| ID | A | B | C |
+-----------+------+-------+-----+
| 1 | 1 | 2 | 3 |
| 2 | 4 | 5 | 6 |
+-----------+------+-------+-----+
下面是我写的 sql 使用 oracle pivot 函数将这些行转置为列。
select * from
(
select ID, TYPE, VALUE from table where TYPE in ('A','B','C')
)
PIVOT (
max(value)
for TYPE in (1 column_a, 2 column_b, 3 column_c)
)
所以这些是我的问题
- 这个 returns 结果是 returns 空值,即使它不为空。它返回 NULL 是因为我正在使用带有字符串值的聚合函数吗?
- 此 SQL 没有将值映射到类型。所以基本上值总是需要绑定到类型列。这对 PIVOT 功能可行吗?
我也在考虑通过将 INSERT 与 SELECT 一起使用来重新创建 table。请告知使用 PIVOT 是否可行。
我发现使用条件聚合更容易:
select id,
max(case when type = 'A' then value end) as a,
max(case when type = 'B' then value end) as b,
max(case when type = 'C' then value end) as c
from t
group by id;
您可以使用 create table as
将结果插入 table。这也应该适用于数据透视查询。
1,2,3 为枢轴应该是 'A','B','C'
with CTE (ID,Type,Value) as (
SELECT 1, 'A',1 from dual union all
SELECT 1, 'B',2 from dual union all
SELECT 1, 'C',3 from dual union all
SELECT 2, 'A',4 from dual union all
SELECT 2, 'B',5 from dual union all
SELECT 2, 'C',6 from dual)
SELECT *
FROM (SELECT ID, TYPE, VALUE FROM cte WHERE TYPE in ('A','B','C'))
PIVOT (sum(value)
for TYPE in ('A' "A",'B' "B",'C' "C")) --Type has values of A,B,C so you have
--to pivot on A,B,C.. 1,2,3 are the values.
它返回 NULL 是因为我正在使用带有字符串值的聚合函数吗? 不,因为你的重点是错误的。以类型为中心而不是类型值
此 SQL 没有将值映射到类型。 *在我看来只是因为你在错误的事情上旋转......
所以基本上值总是需要绑定到类型列。这对 PIVOT 功能可行吗? 是的,在我看来你差了 3 个字符
工作示例的IMG: