IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Missing dates in date range


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
lanand_hps

New User


Joined: 05 Dec 2007
Posts: 82
Location: chennai

PostPosted: Fri Oct 30, 2009 4:43 pm
Reply with quote

Hi,
My requirement is to find out the missing dates in table like

Name Start-date End-date
Name1 1 5
Name1 8 20
Name1 22 31

Assuming the above table has attendance details, Name1 has attended classes from 1 to 5, 8 to 20 and 22 till 31st.
The missing dates(absent) are 6,7,21 which i need to capture using a query.
Please advise.

Thanks,
Anand
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri Oct 30, 2009 5:34 pm
Reply with quote

ask one of your classmates.

use the forum search; this question was asked by the last class.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Fri Oct 30, 2009 7:36 pm
Reply with quote

did a search : didn't find it.(this doesn't mean it's not there, just that i can't find properly)

generating a CTE table containing dates (or a sequence of numbers ) is not that hard with recursive SQL
then a simple not exists should give you a result.
Code:
With allnumber(nbr) as (
   select 1 from sysibm.sysdummy1
union all
   select nbr + 1 from allnumbers where nbr < 99)

Select name1 , nbr from  allnumbers N, table1 A
where not exists (select * from table1 B where A.name1 = B.name1 and B.nfrom <= N.nbr and B.nto >= N.nbr)


If there are no overlaps , following query (without recursive sql) will give you also a workable result :

Code:
select * from
 ( select name1, 'from'  , nto + 1 from table1 t1
   where nto < :max_nto
     and not exists (select * from table1 t2 where t1.name1 = t2.name1 and t2.nfr = (t1.nto +1))
 )  tos_without_from
union all
 ( select name1, 'to  ', nfr - 1 from table1 t1
   where nfr > :min_nfr
     and not exists (select * from table1 t2 where t1.name1 = t2.name1 and t2.nto = (t1.nfr - 1))
 ) froms_without_to
where tos_without_from.name1 = froms_without_to.name1
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Replacing 'YYMMDD' with date, varying... SYNCSORT 3
No new posts To get the count of rows for every 1 ... DB2 3
No new posts Modifying Date Format Using DFSORT DFSORT/ICETOOL 9
No new posts Generate random number from range of ... COBOL Programming 3
No new posts Need to convert date format DFSORT/ICETOOL 20
Search our Forums:

Back to Top