Skip to Main Content
  • Questions
  • Strange behavior in analytic functions with partitions

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Dmitriy.

Asked: September 25, 2019 - 11:43 am UTC

Last updated: September 25, 2019 - 2:05 pm UTC

Version: Oracle Database 11g Release 11.2.0.4.0 - 64bit Production

Viewed 1000+ times

You Asked

Hello, I have met strange behavior that I can't understand.

I have this table

CREATE TABLE test_table
    (id                             NUMBER(10,0) NOT NULL,
    register_date                  DATE DEFAULT sysdate NOT NULL,
    row_id                         NUMBER(10,0) NOT NULL,
    journal_col_id                 NUMBER(10,0) NOT NULL)


-- Indexes for TEST_TABLE

CREATE INDEX idx_test_table_row_id ON test_table
  (
    "ROW_ID" DESC
  )
NOPARALLEL
LOGGING


CREATE INDEX idx_test_table_col_id ON test_table
  (
    journal_col_id  ASC
  )
NOPARALLEL
LOGGING


With following records:
INSERT INTO test_table 
VALUES(13376,TO_DATE('2017-02-22 09:00:07', 'YYYY-MM-DD HH24:MI:SS'),1276,154);
INSERT INTO test_table 
VALUES(11969,TO_DATE('2017-02-13 09:54:35', 'YYYY-MM-DD HH24:MI:SS'),1276,153);
INSERT INTO test_table 
VALUES(11970,TO_DATE('2017-02-13 09:54:40', 'YYYY-MM-DD HH24:MI:SS'),1276,132);
INSERT INTO test_table 
VALUES(11971,TO_DATE('2017-02-13 09:55:16', 'YYYY-MM-DD HH24:MI:SS'),1276,133);
INSERT INTO test_table 
VALUES(11975,TO_DATE('2017-02-13 09:56:49', 'YYYY-MM-DD HH24:MI:SS'),1276,133);
INSERT INTO test_table 
VALUES(11961,TO_DATE('2017-02-13 09:53:57', 'YYYY-MM-DD HH24:MI:SS'),1276,124);
INSERT INTO test_table 
VALUES(11962,TO_DATE('2017-02-13 09:53:57', 'YYYY-MM-DD HH24:MI:SS'),1276,134);
INSERT INTO test_table 
VALUES(11963,TO_DATE('2017-02-13 09:54:01', 'YYYY-MM-DD HH24:MI:SS'),1276,125);
INSERT INTO test_table 
VALUES(11964,TO_DATE('2017-02-13 09:54:16', 'YYYY-MM-DD HH24:MI:SS'),1276,126);
INSERT INTO test_table 
VALUES(11965,TO_DATE('2017-02-13 09:54:22', 'YYYY-MM-DD HH24:MI:SS'),1276,127);
INSERT INTO test_table 
VALUES(11966,TO_DATE('2017-02-13 09:54:25', 'YYYY-MM-DD HH24:MI:SS'),1276,128);
INSERT INTO test_table 
VALUES(11967,TO_DATE('2017-02-13 09:54:30', 'YYYY-MM-DD HH24:MI:SS'),1276,129);



Two following queries return different results:

SELECT id, 
       max(register_date) over (PARTITION BY row_id, journal_col_id) max_date      
  FROM test_table  


ID MAX_DATE
---------- ---------------------------------
13376 22-Feb-2017 09:00:07
11969 22-Feb-2017 09:00:07
11970 22-Feb-2017 09:00:07
11971 22-Feb-2017 09:00:07
11975 22-Feb-2017 09:00:07
11967 22-Feb-2017 09:00:07
11962 22-Feb-2017 09:00:07
11963 22-Feb-2017 09:00:07
11964 22-Feb-2017 09:00:07
11965 22-Feb-2017 09:00:07
11966 22-Feb-2017 09:00:07
11961 22-Feb-2017 09:00:07


SELECT id,       
       max(register_date) over (PARTITION BY journal_col_id,  row_id ) max_date
  FROM test_table  


ID MAX_DATE
---------- ---------------------------------
11961 13-Feb-2017 09:53:57
11963 13-Feb-2017 09:54:01
11964 13-Feb-2017 09:54:16
11965 13-Feb-2017 09:54:22
11966 13-Feb-2017 09:54:25
11967 13-Feb-2017 09:54:30
11970 13-Feb-2017 09:54:40
11971 13-Feb-2017 09:56:49
11975 13-Feb-2017 09:56:49
11962 13-Feb-2017 09:53:57
11969 13-Feb-2017 09:54:35
13376 22-Feb-2017 09:00:07


I found the reason for that are indexes, but why, I can't explain.

Product Version: Oracle Database 11g Release 11.2.0.4.0 - 64bit Production
In LiveSQL I can't repeat this.

and Chris said...

Yep, this looks like a bug.

But it's working as expected in 12.1. So if this is causing major issues for you, it looks like a good excuse to upgrade :)

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Dmitriy Kukushkin, September 26, 2019 - 4:51 am UTC


More to Explore

Analytics

Analytic SQL got you confused? Check out Connor McDonald's complete video course.