Duplicate rows returned with Query Rewrite for Distinct query (Doc ID 2128461.1)

Last updated on APRIL 19, 2016

Applies to:

Oracle Database - Enterprise Edition - Version 11.2.0.3 to 12.1.0.1 [Release 11.2 to 12.1]
Information in this document applies to any platform.

Symptoms

Master table contains the following rows:

SQL> select * from tab1;

PK COL1 COL2
---------- ---------- --------------------
1 a hello there
2 a hello sir
3 b bye

Materialized view TAB1_MV is defined in the following way with GROUP BY
clause on col1 and col2 of the master table TAB1:
create materialized view tab1_mv build immediate
refresh complete on demand enable query rewrite
as
select col1, col2, count(*) as cnt
from tab1
group by col1, col2;

Without rewriting to the mview the DISTINCT query below returns the desired
result:

SQL> Select /*+ NO_REWRITE */ DISTINCT col1, substr(col2,1,5) from tab1;

COL1 SUBSTR(COL2,1,5)
---------- --------------------
b bye
a hello



Cause

Sign In with your My Oracle Support account

Don't have a My Oracle Support account? Click to get started

My Oracle Support provides customers with access to over a
Million Knowledge Articles and hundreds of Community platforms