带联合的别名表都不起作用

时间:2017-11-21 07:55:29

标签: sql oracle column-alias

我在一个简单的查询中有ora-00094(标识符无效),但我看不出原因。你能帮帮我吗?

select columnA, 'More than 4000 bytes'
from tableA

union all

select  p.columnB, listagg(p.columnC, ',') within group (order by p.columnC)
from (            
  select distinct b.job_name, a.hostname
  from tableB a, emuser.def_job b
) p   
group by p.columnB 
order by p.columnB desc;

1 个答案:

答案 0 :(得分:1)

ORDER BY用于整个查询的ResultSet。所以对于ORDER BY,这里没有columnB。 ResultSet只有第一个查询的列名。

试试这个

SELECT columnA, 'More than 4000 bytes' as columnC  FROM tableA
UNION ALL
  SELECT p.columnB, LISTAGG (p.columnC, ',') WITHIN GROUP (ORDER BY p.columnC)
    FROM (SELECT DISTINCT b.job_name, a.hostname
            FROM tableB a, emuser.def_job b) p
GROUP BY p.columnB
ORDER BY p.columnA DESC;