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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

selecting special days and counting

gambro2Nov 4 2010 — edited Nov 4 2010
Dear all,
I need to select certain dates and following days with the same criteria.
An example would be:

CREATE TABLE DEMO
(
EMP_ID NUMBER NOT NULL,
WORKING_DAY DATE NOT NULL,
ACTIVE_CODE VARCHAR2 (1) NULL,
CONSTRAINT DEMO_PK
PRIMARY KEY(EMP_ID, WORKING_DAY)
USING INDEX
)


INSERT INTO DEMO( EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES( 123, TO_DATE('01.11.2010 00:00:00'), '_');
INSERT INTO DEMO( EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES( 123, TO_DATE('02.11.2010 00:00:00'), '_');
INSERT INTO DEMO( EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES( 123, TO_DATE('03.11.2010 00:00:00'), 'K');
INSERT INTO DEMO( EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES( 123, TO_DATE('04.11.2010 00:00:00'), 'K');
INSERT INTO DEMO( EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES( 123, TO_DATE('05.11.2010 00:00:00'), 'K');
INSERT INTO DEMO( EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES( 123, TO_DATE('06.11.2010 00:00:00'), NULL);
INSERT INTO DEMO( EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES( 123, TO_DATE('07.11.2010 00:00:00'), NULL);
INSERT INTO DEMO( EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES( 123, TO_DATE('08.11.2010 00:00:00'), 'K');
INSERT INTO DEMO( EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES( 123, TO_DATE('09.11.2010 00:00:00'), '_');
INSERT INTO DEMO( EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES( 123, TO_DATE('10.11.2010 00:00:00'), 'K');
INSERT INTO DEMO( EMP_ID, WORKING_DAY, ACTIVE_CODE) VALUES( 123, TO_DATE('11.11.2010 00:00:00'), '_');

Now I'm looking for the first day with Active_Code ='K' and want to count the following days that have 'K', too.
Expected result: November 3rd -> 4 Ks (because the 8th is after the weekend); November 10th -> 1K

I hope the example/explanation isn't too bad...

I have built a procedure that would do the counting of the following occurances, if I give the first occurance (here 3rd and 8th).
But findign the first 'K' is my problem.
One solution would be:
AND
(SELECT T2.ACTIVE_CODE FROM DEMO T2
WHERE T2.EMP_ID= T1.EMP_ID AND T2.WORKING_DAY= T1.WORKING_DAY-1) <> 'K'
AND
(SELECT T2.ACTIVE_CODE FROM DEMO T2
WHERE T2.EMP_ID= T1.EMP_ID AND T2.WORKING_DAY= T1.WORKING_DAY-2) <> 'K'
AND
(SELECT T2.ACTIVE_CODE FROM DEMO T2
WHERE T2.EMP_ID= T1.EMP_ID AND T2.WORKING_DAY= T1.WORKING_DAY-3) <> 'K'

But it's damn imperformant if I'm dealing with millions of records :-(

Could anyone please give me a hint on how to solve this?
Thank you.

Best Regards
Christian
This post has been answered by Frank Kulash on Nov 4 2010
Jump to Answer

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 2 2010
Added on Nov 4 2010
16 comments
466 views