MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help with coding complex SQL (DB2 z/OS v8 CM)

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
grimpeur
Beginner


Joined: 10 Jul 2006
Posts: 16
Topics: 5

PostPosted: Mon Apr 07, 2008 6:02 am    Post subject: Help with coding complex SQL (DB2 z/OS v8 CM) Reply with quote

Hi everyone,

Tearing my hair out a little bit with this one, if anybody is able to offer suggestions it would be most appreciated... It sounds fairly simple on the face of it, but I'm having trouble with the coding.

I'm trying to interrogate SYSIBM.SYSCOPY (to create a list of Modify Recovery statements) at DBNAME/TSNAME/DSNUM level.

Our rules are:-
1) If the timestamp of the latest Full IC for a db/ts/dsnum combo is over 3 months old, return a row with the date of that timestamp.
2) If the timestamp of the latest Full IC is under 3 months old, find and return a row with the date of the most recent syscopy row that is over 3 months old of any type for that db/ts/dsnum.
3). If no Full IC exists, or there are no entries of any type over 3 months old, a row is not required in the result set for that db/ts/dsnum.
4) Must work at v8 CM.

The (incomplete) query is shown below to demonstrate the relevant fields - at present this just returns the latest FIC timestamp for each DB/TS/DSNUM combo where a FIC exists. I've tried adding self joins, correlated sub-queries, case statements etc to this, but cannot get the desired results.

SELECT DBNAME, TSNAME, DSNUM, MAX(TIMESTAMP)
FROM SYSIBM.SYSCOPY
WHERE ICTYPE = 'F'
GROUP BY DBNAME, TSNAME, DSNUM
WITH UR;

We have some SYSCOPY tables with record counts in the millions, so reasonable performance is important.

Thanks in advance, your suggestions are eagerly anticipated!
Back to top
View user's profile Send private message
videlord
Beginner


Joined: 09 Dec 2004
Posts: 147
Topics: 19

PostPosted: Mon Apr 07, 2008 9:35 am    Post subject: Reply with quote

Code:

   select dbname, tsname, dsnum, max(TIMESTAMP)       
    from sysibm.syscopy                               
   where timestamp <  current timestamp - 3 months     
      and ICTYPE = 'F'                                 
   group by dbname, tsname, dsnum                     
Back to top
View user's profile Send private message
grimpeur
Beginner


Joined: 10 Jul 2006
Posts: 16
Topics: 5

PostPosted: Mon Apr 07, 2008 9:39 am    Post subject: Reply with quote

Hi Videlord,
Thanks for your reply... however, your SQL does not meet the requirements of all the rules. Please let me know if they are not clear.
Any other takers for this SQL challenge???
Thanks,
Grimp.
Back to top
View user's profile Send private message
videlord
Beginner


Joined: 09 Dec 2004
Posts: 147
Topics: 19

PostPosted: Mon Apr 07, 2008 9:51 am    Post subject: Reply with quote

just noticed the 2nd requiement are any type
Code:

  select dbname, tsname, dsnum, max(TIMESTAMP)         
   from sysibm.syscopy                                 
  where timestamp <  current timestamp - 3 months       
     and ICTYPE = 'F'                                   
  group by dbname, tsname, dsnum                       
                                                       
  union all                                             
                                                       
  select dbname, tsname, dsnum, max(TIMESTAMP)         
    from sysibm.syscopy                                 
   where timestamp <  current timestamp - 3 months     
   and   exists (select 1 from sysibm.syscopy           
      where timestamp >  current timestamp - 3 months   
        and ICTYPE = 'F' )                             
   group by dbname, tsname, dsnum                             
Back to top
View user's profile Send private message
videlord
Beginner


Joined: 09 Dec 2004
Posts: 147
Topics: 19

PostPosted: Mon Apr 07, 2008 9:52 am    Post subject: Reply with quote

remove duplicates
Code:

  select dbname, tsname, dsnum, max(TIMESTAMP)               
   from sysibm.syscopy                                       
  where timestamp <  current timestamp - 3 months           
     and ICTYPE = 'F'                                       
     and not  exists (select 1 from sysibm.syscopy           
         where timestamp >  current timestamp - 3 months     
           and ICTYPE = 'F' )                               
  group by dbname, tsname, dsnum                             
                                                             
  union all                                                 
                                                             
  select dbname, tsname, dsnum, max(TIMESTAMP)               
    from sysibm.syscopy                                     
   where timestamp <  current timestamp - 3 months           
   and   exists (select 1 from sysibm.syscopy               
      where timestamp >  current timestamp - 3 months       
        and ICTYPE = 'F' )                                   
   group by dbname, tsname, dsnum                           
Back to top
View user's profile Send private message
grimpeur
Beginner


Joined: 10 Jul 2006
Posts: 16
Topics: 5

PostPosted: Tue Apr 08, 2008 7:54 am    Post subject: Reply with quote

Thanks very much for your assistance Videlord. It needed a little tweaking, but is now working as desired.
Cheers!
Grimps.
Back to top
View user's profile Send private message
grimpeur
Beginner


Joined: 10 Jul 2006
Posts: 16
Topics: 5

PostPosted: Tue Apr 08, 2008 8:01 am    Post subject: Reply with quote

Thanks very much for your assistance Videlord. It needed a little tweaking, but is now working as desired.
Cheers!
Grimps.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group