SQL Query

I have table having records with numbers and dates ... as like below.
I want to find out what are the missing dates for a perticular month of that col1 numbers.


create table tab_new(
col1 number,
col2 date,
col3 number );

insert into  tab_new values( 1,'01-JAN-2011',20);
insert into  tab_new values(1,'02-JAN-2011',30);

Reocrds in table look as below:
COL1      COL2      COL3
----------------------------
1      1/1/2011      20
1      1/2/2011      30

Expected OutPut as below:

COL1      Missing Date
---------------------
1      1/3/2011
1      1/4/2011
1      1/5/2011
1      1/6/2011
1      1/7/2011
1      1/8/2011
1      1/9/2011
1      1/10/2011
1      1/11/2011
..
..
..
1      1/31/2011
LVL 17
Swadhin RaySenior Technical Engineer Asked:
Who is Participating?
 
slightwv (䄆 Netminder)Connect With a Mentor Commented:
See if this will work for you.
select col1, day_of_year
from
(select distinct col1 from tab_new) tab_new,
(select trunc(sysdate,'YYYY') + level-1 day_of_year
from dual
connect by level <= last_day(to_date('12/2011','MM/YYYY'))-trunc(to_date('01/2011','MM/YYYY'),'MM')+1
)
minus
select col1, col2 from tab_new
/

Open in new window

0
 
slightwv (䄆 Netminder) Commented:
If you know you are after col1=1, then you really don't need to select it, just hard code it.

To get the list of missing days, just generate the full list of days and use the MINUS query.

Check out the sample below.
drop table tab_new purge;
create table tab_new(
col1 number,
col2 date, 
col3 number );

insert into  tab_new values( 1,to_date('01-JAN-2011','DD-MON-YYYY'),20);
insert into  tab_new values(1,to_date('02-JAN-2011','DD-MON-YYYY'),30);



select trunc(to_date('01/2011','MM/YYYY'),'MM') + level-1 
from dual
connect by level <= last_day(to_date('01/2011','MM/YYYY'))-trunc(to_date('01/2011','MM/YYYY'),'MM')+1
minus
select col2 from tab_new where col1=1
/

Open in new window

0
 
Swadhin RaySenior Technical Engineer Author Commented:
This is correct but I also need the col1 with dates missing data ...

boz I have multiple data in col1 like 1 ,2,3 ..
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
slightwv (䄆 Netminder) Commented:
>>but I also need the col1 with dates missing data ...

I'm still a little confused.  How are you going to get the initial Month/Year combination and column1 to select?

I'm guessing that you don't have a starting point and want to generate the list for ALL rows in your table regardless of date and col1?

Please add to your sample data and expected results.

0
 
Swadhin RaySenior Technical Engineer Author Commented:
Please find the sample insert data for the table :

 
begin
insert into  tab_new values( 1,'01-JAN-2011',20);
insert into  tab_new values(1,'02-JAN-2011',30);
insert into  tab_new values(2,'03-JAN-2011',40);
insert into  tab_new values(2,'06-JAN-2011',90);
insert into  tab_new values(3,'07-JAN-2011',80);
insert into  tab_new values(3,'02-JAN-2011',60);
insert into  tab_new values(3,'06-JAN-2011',40);
insert into  tab_new values(5,'02-JAN-2011',50);
insert into  tab_new values(5,'06-JAN-2011',50);
insert into  tab_new values(5,'11-JAN-2011',60);
insert into  tab_new values(5,'20-JAN-2011',30);
insert into  tab_new values(5,'30-JAN-2011',40);
insert into  tab_new values(6,'08-JAN-2011',40);
insert into  tab_new values(6,'09-JAN-2011',70);
insert into  tab_new values(6,'31-JAN-2011',50);
insert into  tab_new values(8,'01-JAN-2011',10);
insert into  tab_new values(8,'11-JAN-2011',20);
end;

Open in new window



Now for col1 we have data like 1,2,3,5,6 and 8 with col2 as dates.

Now I want a list of all missing dates for the same year for each record i.e. col1

Lets take an example for col1 for record 1 data has only 2 dates i.e 1st jan and 2nd jan for year 2011.
Now we need all the missing dates for year 2011 , so except 1st and 2nd date all the dates should be displayed and like wise for others.
So for record 1 we will have 365 days -2 days having the date.

So now the expected result will look like below:

col1      missing_date
-----------------------------
1         3-Jan-2011
1         4-Jan-2011
...........
.......
...
2       1-Jan-2011
2       2-Jan-2011

...
...
..
.And like wise...
0
 
slightwv (䄆 Netminder) Commented:
Still not fully understanding.

What are the results if I add the following record to the above list (notice the 2010 year):
insert into  tab_new values( 1,'01-JAN-2010',20);


Can you explain the requirements verbally?  Do you want to select the MIN and MAX date for a distinct col1 and then find the missing records for ALL years involved?
0
 
Swadhin RaySenior Technical Engineer Author Commented:
The data will be there only for this year i.e only for one year the records will be present.
0
 
Swadhin RaySenior Technical Engineer Author Commented:
Thanks.. a lot
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.