Solved

SQL Query

Posted on 2011-09-06
8
239 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Queries 15 48
Field name with special character (Ñ) in Oracle 11 95
Creation date for a PDB 5 40
Input a SQl, output tables amd columns used in the SQL 19 55
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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

820 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