A union of views works, but when I create a view out of a union of views, it fails.

huangapple go评论129阅读模式
英文:

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 子句。

要么将每个子查询更改为以下之一:

  1. SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT,
           M_ID,
           COUNT(DISTINCT OBJECT_ID) as RESULT
    FROM   VIEW1
    GROUP BY M_ID
    
  2. 或者使用分析函数生成计数:

    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; and
  • M_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:

  1. SELECT SYS_EXTRACT_UTC(SYSTIMESTAMP) as DT,
           M_ID,
           COUNT(DISTINCT OBJECT_ID) as RESULT
    FROM   VIEW1
    GROUP BY M_ID
    
  2. 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 UNIONing together.

huangapple
  • 本文由 发表于 2023年6月9日 03:11:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定