For the cost column values from a table course, try to pivot into a table of how many times a cost values happens, e.g $1095 happens 3 times.
1095 1195 1595 NULL
---------- ---------- ---------- ----------
3 25 1 1
Cost values
select cost from course
order by cost;
COST
----------
1095
1095
1095
1195
...
1195
1595
NULL
This works.
WITH counter AS (
SELECT
cost
FROM course
)
select * from counter
pivot (
count(*)
for (cost)
in (1095, 1195, 1595, NULL)
);
1095 1195 1595 NULL
---------- ---------- ---------- ----------
3 25 1 1
This does not.
select cost from course
pivot (
count(*)
for (cost)
in (1095, 1195, 1595, NULL)
);
ORA-00904: "COST": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 178 Column: 8
Question
It seems to me both selecting cost
and doing count(*)
but the former worked but the latter did not. Please help understand why.
question from:
https://stackoverflow.com/questions/65870160/sql-explanation-on-pivot-behavior 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…