I have a complex oracle view which returns data which has logical duplicates within the rows returned. My goal is to retrieve only one row when duplicates are found based two columns (text and datetime), but to decide which one of the duplicates to return will be based on a third column (datetime).
I have simulated the result set below into a table with stubbed data (as found here on SQLFiddle):
CREATE TABLE TimeTable (
ID number NOT NULL,
NAME VARCHAR2(20) NOT NULL, -- Grouped by this first
TARGETVALUE INT, -- ultimate target value to be returned (no precedence from this value)
NOTE VARCHAR2(20) NULL, -- Just a note for the developer on StackOverflow
BEGIN_DATE TIMESTAMP NOT NULL, -- Grouped by this 2nd (down to the minute, not seconds)
APPROVAL_DATE TIMESTAMP NOT NULL -- Decides the ties for duplicates
);
insert into TimeTable (ID, NAME, TARGETVALUE, NOTE, BEGIN_DATE, APPROVAL_DATE) values
(1, 'Alpha', 5, 'Duplicate First', '08-MAR-14 09.43.00.000000000',
'09-MAR-14 09.43.00.000000000');
insert into TimeTable (ID, NAME, TARGETVALUE, NOTE, BEGIN_DATE, APPROVAL_DATE) values
(2, 'Alpha', 2, 'Duplicate Middle', '08-MAR-14 09.43.00.000000000',
'09-MAR-14 09.43.00.000000000');
insert into TimeTable (ID, NAME, TARGETVALUE, NOTE, BEGIN_DATE, APPROVAL_DATE) values
(3, 'Alpha', 3, 'Final Target', '08-MAR-14 09.43.00.000000000',
'09-MAR-14 10.00.00.000000000');
-- Same time as alpha, but not related.
insert into TimeTable (ID, NAME, TARGETVALUE, NOTE, BEGIN_DATE, APPROVAL_DATE) values
(4, 'Beta', 4, 'Only Target', '08-MAR-14 09.43.30.000000000',
'09-MAR-14 11.00.30.000000000');
The result set which is needed would be 2 rows of
3, 'Alpha', 3, '08-MAR-14 09.43.00.000000000', '09-MAR-14 10.00.00.000000000'
4, 'Beta', 4, '08-MAR-14 09.43.30.000000000' '09-MAR-14 11.00.30.000000000'
Note for clarification if I had this value in the database
5, 'Alpha', 8, '09-MAR-14 09.43.00.000000000', '12-MAR-14 10.00.00.000000000'
Then that Alpha set would be unique and also returned because it is not considered a duplicate due to the different BEGIN_DATE
(which is the 9th of March not 8th).
Here are the rules to follow
- The
NAME
relates data. - The
BEGIN_DATE
is the second relation where exact times up to the minute will have duplicates which need to be weeded out to be based on #3. - If there are duplicates per #1 and #2, then they will be removed as determined by the latest
APPROVAL_DATE
which will win over the earlier dates.