Link to home
Start Free TrialLog in
Avatar of Swadhin Ray
Swadhin RayFlag for United States of America

asked on

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
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

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

Avatar of Swadhin Ray

ASKER

This is correct but I also need the col1 with dates missing data ...

boz I have multiple data in col1 like 1 ,2,3 ..
>>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.

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...
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?
The data will be there only for this year i.e only for one year the records will be present.
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.. a lot