gpt4 book ai didi

sql - 甲骨文 SQL : How to INSERT a SELECT statement with a GROUP BY clause on a table with IDENTITY column?

转载 作者:太空狗 更新时间:2023-10-30 01:51:34 24 4
gpt4 key购买 nike

在一个应用程序中,我打算在 Oracle 12c 数据库上进行截断和插入操作,但在 IDENTITY 列中发现了这个问题。尽管 INSERT...SELECT 语句适用于我尝试过的大多数 SELECT 使用,但当此语句也有一个 GROUP BY 子句时,它无法工作,发出“ORA-00979:不是 GROUP BY 表达式”投诉。下面是一些示例代码:

create table aux (
owner_name varchar2(20),
pet varchar2(20) );

insert into aux values ('Scott', 'dog');
insert into aux values ('Mike', 'dog');
insert into aux values ('Mike', 'cat');
insert into aux values ('John', 'turtle');

create table T1 (
id number generated always as identity,
owner_name varchar2(20),
pet_count number
);
insert into T1 (owner_name, pet_count)
select owner_name, count(*) as pet_count from aux group by owner_name;
select owner_name, count(*) as pet_count from aux group by owner_name;

它在第一次插入时有效,但在下一次插入时失败。

编辑:我已经更改了代码,因此问题更容易理解,同时仍然可以重现。

感谢您的帮助!

最佳答案

在 Oracle 社区中,这个问题已经得到解答。 https://community.oracle.com/message/13227544#13227544

insert into T1 (owner_name, pet_count)
with t as (select /*+ materialize */ owner_name, count(*) as pet_count from aux group by owner_name)
select owner_name, pet_count from t

引用原始答案,请记住没有记录物化提示。感谢大家的帮助!

关于sql - 甲骨文 SQL : How to INSERT a SELECT statement with a GROUP BY clause on a table with IDENTITY column?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31731196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com