Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Query

Posted on 2011-09-06
8
Medium Priority
?
267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 77

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 77

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 77

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 77

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

688 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