简体   繁体   中英

Why SELECT DISTINCT returns twice the same value?

I have a table in Oracle, which has DATE column. I can observe that SELECT DISTINCT returns duplicates:

select distinct myDate from myTable;

myDate
----------------------------
2009-09-05 00:00:00          
2009-09-05 00:00:00

But:

select distinct to_char(myDate, 'DD-MM-YYYY HH24:MI:SS') from myTable;

TO_CHAR(myDate,'DD-MM-YYYYHH24:MI:SS')
------------------------------------------------
05-09-2009 00:00:00

And next queries, which can tell more:

select count(*) from myTable;

  COUNT(*)
----------
        12 

select myDate, count(*) from myTable group by myDate;

myDate                        COUNT(*)
---------------------------- ----------
2009-09-05 00:00:00                   6 
2009-09-05 00:00:00                   6 


select count(*) from myTable where myDate='2009-09-05';

  COUNT(*)
----------
         6 

What can be the reason of such behaviour? As far as I know there is no more information stored in DATE column which can make the difference (no time zone, no miliseconds).


EDIT: table DDL

Unfortunately I can't post whole DDL. Definition of the column is:

myDate DATE NOT NULL

What can be important, table is partitioned using this column:

PARTITION BY RANGE (myDate)
(
PARTITION P_19991231 VALUES LESS THAN 
    (TO_DATE('1999-12-31 00:00:00',
           'YYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))
);

There is more partitions in the table.

And also ENABLE ROW MOVEMENT is ON.

Problem was in partitioning. I made few EXCHANGE PARTITIONS operations without validation and as a result few records was placed on incorrect partition (not in its range). In such situation comparisons and SELECT DISTINCT didn't work correct.

A precision problem: after the seconds, you have milliseconds. You have in your table 2 rows with dates in the same second, but not with the same milliseconds. When you execute a SELECT DISTINCT against the dates, they are distinct, so you have 2 rows. But using the to_char function, you round the dates to the seconds and you have only one row returned.

To group by dates, you must be sure that the date is exactly the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM