在数据透视期间指定列时标识符无效 - ORA-00904
Invalid Identifier When Specifying Columns During Pivot - ORA-00904
尝试创建数据透视表时,我在以下查询中收到 ORA-00904 invalid identifier
错误。
Table定义
create table table1(id int, name varchar2(20), col1 int);
insert into table1 values(1, 'Alex', 99);
insert into table1 values(2, 'Alex', 98);
insert into table1 values(3, 'James', 97);
insert into table1 values(4, 'Eric', 99);
insert into table1 values(5, 'Stan', 99);
错误查询
select name, col1
from table1
pivot (count(name) for col1 in (99, 98, 97))
;
但是,以下查询将起作用
工作查询 1
select *
from table1
pivot (count(name) for col1 in (99, 98, 97)) p
;
工作查询 2
with cte as (
select name, col1
from table1
)
select *
from cte
pivot (count(name) for col1 in (99, 98, 97))
;
我更喜欢工作查询 2 的输出,因为我得到的是不包括所有其他数据的计数
99 | 98 | 97
-------|----|-------
3 | 1 | 1
为什么在直接从 table 指定列时尝试旋转时会发生错误?
在pivot
之后,name
不再出现在结果集中。它已被名称如“99”的计数和列所取代。
这就是为什么人们经常将 select *
与 pivot
一起使用的原因。大多数列已在 in
子句中列出。
尝试创建数据透视表时,我在以下查询中收到 ORA-00904 invalid identifier
错误。
Table定义
create table table1(id int, name varchar2(20), col1 int);
insert into table1 values(1, 'Alex', 99);
insert into table1 values(2, 'Alex', 98);
insert into table1 values(3, 'James', 97);
insert into table1 values(4, 'Eric', 99);
insert into table1 values(5, 'Stan', 99);
错误查询
select name, col1
from table1
pivot (count(name) for col1 in (99, 98, 97))
;
但是,以下查询将起作用
工作查询 1
select *
from table1
pivot (count(name) for col1 in (99, 98, 97)) p
;
工作查询 2
with cte as (
select name, col1
from table1
)
select *
from cte
pivot (count(name) for col1 in (99, 98, 97))
;
我更喜欢工作查询 2 的输出,因为我得到的是不包括所有其他数据的计数
99 | 98 | 97
-------|----|-------
3 | 1 | 1
为什么在直接从 table 指定列时尝试旋转时会发生错误?
在pivot
之后,name
不再出现在结果集中。它已被名称如“99”的计数和列所取代。
这就是为什么人们经常将 select *
与 pivot
一起使用的原因。大多数列已在 in
子句中列出。