我在Oracle 11g中搜索了有关数据透视的Stackoverflow,但找不到适合我情况的语法。
以下是我的查询(在Oracle 11g中运行)。
select
age,
gender,
sum(hours) as hours
from table1
group by age, gender
这是O / P结果
age gender hours
25 Male 10
55 Female 5
45 Female 12
......这是我想要的O / P结果
Age Male Female
25 10 0
45 0 12
55 0 5
...这是我的查询
select *
from
(
select
age,
gender,
sum(hours) as hours
from table1
)
pivot (
sum (hours) for gender in (
Male as 'Male',
Female as 'Female')
)
......这是错误:
ORA-00931: missing identifier
有人可以请教育我吗?
答案 0 :(得分:4)
你很亲密:
select age, nvl(male, 0) male, nvl(female, 0) female
from
(
select age, gender, sum(hours) as hours
from table1
group by age, gender
)
pivot
(
sum(hours) for gender in ('Male' as male, 'Female' as female)
);
答案 1 :(得分:0)
缺少标识符的原因'错误是 - 预期的语法是
<Value> As <identifier>
所以,当你给予 - 男性为&#39;男性&#39; ,它&#39;之后找不到标识符。因此给出了这个错误。
即使你纠正了这个错误,下一个错误也会变成&#34;而不是单组组功能&#34;因为你选择年龄和性别以及总计函数总和(小时)而没有分组(我刚才提到这是一个技术点,你实际上并不需要这里的任何组)。
请参阅以下代码了解您的输出。
SELECT age,nvl(male,0) AS male,nvl(female,0) AS frmale FROM
(SELECT *
FROM (
SELECT age,gender,hours
FROM test_subha3)
pivot (sum(hours) FOR gender IN ('Male' AS Male,'Female' AS Female))
)