英文:
Why is that a union of views works but when I create a view out of union of view, it fails
问题
在ORACLE中,我正在尝试创建一个视图,该视图是多个视图的联合:
CREATE VIEW "TEST_ALL"
AS
( SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW1
UNION ALL
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW2
UNION ALL
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW3
UNION ALL
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW4
UNION ALL
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW5
UNION ALL
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW6
)
这不起作用吗?然而,当我移除M_ID
时,它可以创建该视图。
另外,当我只联合所有内部视图时,它可以正常工作。有人可以帮忙吗?
英文:
In ORACLE, I am trying to create a view which is a union of view:
CREATE VIEW "TEST_ALL"
AS
( SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW1
UNION ALL
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW
UNION ALL
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW3
UNION ALL
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW4
UNION ALL
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW5
UNION ALL
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW6
)
This does not work? However when I remove M_ID
, it creates the view.
Also when I just union all the inner views it works. Can someone please help?
答案1
得分: 1
In the query:
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT,
M_ID,
COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW1
Then:
SYS_EXTRACT_UTC(SYSTIMESTAMP)
是所有行中的有效常量;COUNT(DISTINCT OBJECT_ID)
是一项聚合操作;M_ID
是一个未聚合的列。
您混合了聚合和未聚合的列,并且没有使用 GROUP BY
子句。
要么将每个子查询更改为以下之一:
-
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT FROM VIEW1 GROUP BY M_ID
-
或者使用分析函数生成计数:
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) OVER () as RESULT FROM VIEW1
然后对您要进行 UNION
的每个视图重复这个操作。
英文:
In the query:
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT,
M_ID,
COUNT(DISTINCT OBJECT_ID) as RESULT
FROM VIEW1
Then:
SYS_EXTRACT_UTC(SYSTIMESTAMP)
is effectively constant for all the rows;COUNT(DISTINCT OBJECT_ID)
is an aggregation; andM_ID
is an un-aggregated column.
You are mixing aggregated and un-aggregated columns and do not have a GROUP BY
clause.
Either change each sub-query to:
-
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) as RESULT FROM VIEW1 GROUP BY M_ID
-
Or use an analytic function to generate the count:
SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT, M_ID, COUNT(DISTINCT OBJECT_ID) OVER () as RESULT FROM VIEW1
And repeat for each of the views you ar UNION
ing together.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论