Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Query

Posted on 2011-09-06
8
Medium Priority
?
274 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:Swadhin Ray
  • 4
  • 4
8 Comments
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36488226
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
 
LVL 16

Author Comment

by:Swadhin Ray
ID: 36488339
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
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36488364
>>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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 16

Author Comment

by:Swadhin Ray
ID: 36489116
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
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36489512
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
 
LVL 16

Author Comment

by:Swadhin Ray
ID: 36489638
The data will be there only for this year i.e only for one year the records will be present.
0
 
LVL 78

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 total points
ID: 36489947
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
 
LVL 16

Author Closing Comment

by:Swadhin Ray
ID: 36493592
Thanks.. a lot
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Via a live example, show how to take different types of Oracle backups using RMAN.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question