Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to INSERT a SELECT statement with a GROUP BY clause on a table with IDENTITY column?

h.tercerosJul 30 2015 — edited Jul 30 2015

n an application, I intend on truncating and inserting on an Oracle 12c database, but have found this issue with an IDENTITY column. Even though the INSERT... SELECT statement works on most SELECT uses I have tried, when this statement also has a GROUP BY clause, it fails to work, issuing a "ORA-00979: not a GROUP BY expression" complaint. Below is some example code:

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 );

select owner_name, count(*) as pet_count from aux group by owner_name; -- works just fine

insert into T1 (owner_name, pet_count) select owner_name, count(*) as pet_count from aux group by owner_name; -- doesn't work

The select statement works by itself, but fails as a INSERT ... SELECT statement.

Appreciate the help!

This post has been answered by Solomon Yakobson on Jul 30 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 27 2015
Added on Jul 30 2015
1 comment
4,938 views