-1

I have the below data for 2 Securities as illustration, there are 2 tables where i want to check gaps

Price Table --- Time Series Format only Weekdays data

Sec ID    Date       Price
1       01-Jan-2014  10
1       02-Jan-2014  --- gap in price record not exist in price table
1       03-Jan-2014  --- gap in price record not exist in price table
1       04-Jan-2014  Saturday 
1       05-Jan-2014  Sunday
1       06-Jan-2014  --- gap in price record not exist in price table
1       07-Jan-2014  11

Output Needed gap in Price from 02-Jan till 06-Jan-2014

Shares Table --- Data stored in HOC Format

Sec ID      Start        End          Shares
1          01-Jan-2013  31-Dec-2013    100
1          07-Jan-2014  31-Jan-2014    105

Output needed gap in shares from 01-Jan-2014 till 06-Jan-2014 (weekend should be included)

I have calendar Table available with me which has all weekdays and excluded weekend

Please advise for efficent query

Thanks Hitesh

1 Answer 1

0

You wrote your question in bad way, but I'll try to guess what you need.

so I created dummy table this way:

  create table nk_first_one(
  sec_id NUMBER,
  price_date date,
  price VARCHAR2(50)
  );

  insert into nk_first_one values(1, '01-Jan-2014', '10');
  insert into nk_first_one values(1, '04-Jan-2014', 'Saturday');
  insert into nk_first_one values(1, '05-Jan-2014', 'Sunday');
  insert into nk_first_one values(1, '07-Jan-2014', '11');

And here is simple query which counts each missing day for every row in table:

  select sec_id, 
  price_date, 
  lag(PRICE_DATE, 1, PRICE_DATE) over(order by PRICE_DATE) prevoius_price_date,
  (price_date - lag(PRICE_DATE, 1, PRICE_DATE) over(order by PRICE_DATE)) price_days_gap
  from nk_first_one;

Here is result of that query:

      SEC_ID PRICE_DATE PREVOIUS_PRICE_DATE PRICE_DAYS_GAP
  ---------- ---------- ------------------- --------------
           1 01-JAN-14  01-JAN-14                        0 
           1 04-JAN-14  01-JAN-14                        3 
           1 05-JAN-14  04-JAN-14                        1 
           1 07-JAN-14  05-JAN-14                        2 

So for each row you have number of days from previous price assigned (and as far as I understand you need those with PRICE_DAYS_GAP > 1).

Is that what you wanted? For the second query you can go the same way. Oracle analytic functions are easy to understand :)

3
  • Not Helpful...I need a query which gives me gap date (not gap days count)...so my final output should be Gap Start '02-Jan-2014' and Gap End '06-Jan-2014' Apr 17, 2014 at 11:03
  • are you serious? So you are too lazy to just add one similar line to the query... Apr 18, 2014 at 8:03
  • You have PREVIOUS_PRICE_DATE and it is your gap start date if PRICE_DAYS_GAP > 1 ... If you don't understand it - I don't know what to say.... Apr 18, 2014 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.