Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

need to find sum of quarterly claims?

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
kanchanchouhan

New User


Joined: 05 Sep 2007
Posts: 11
Location: kolkata

PostPosted: Wed Aug 11, 2010 6:51 am    Post subject: need to find sum of quarterly claims?
Reply with quote

i have an Employee table with 3 columns:emp, timestap and claim.
Code:
emp  timestamp  claim
a    jan-2009    c1
b    feb-2009    c1
c    march-2009  c1
a    feb-2010    c2
b    may-2010    c2
c    jan-2010    c2


if i need to find sum of quarterly claims on an yearly basis how can achieve that? do i need to strip the timestamp first and then on the basis of year i can get it or
Code:
select sum(claim)
  from employee
 where  month in('jan-2009','feb-2009', march-2009')


is it right?
Back to top
View user's profile Send private message

prem_ibm

New User


Joined: 15 Sep 2007
Posts: 31
Location: Chennai

PostPosted: Wed Aug 11, 2010 11:29 am    Post subject: Reply to: need to find sum of quarterly claims?
Reply with quote

Hi
Please find below query.....not tested but hope it works...where as to find the Sum of any three rows....then u need to use IN BETWEEN clause....in that u need to provide the range of columns.


SELECT SUM(CLAIM) FROM EMPLOYEE ORDER BY TIEMSTAMP WHERE COUNT(*) = '3';
it gives SUM of first three rows.
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Thu Aug 12, 2010 12:24 am    Post subject:
Reply with quote

Hello,

This "solution" does not appear to address the requirement. . .
Back to top
View user's profile Send private message
sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1013
Location: India

PostPosted: Thu Aug 12, 2010 7:38 am    Post subject:
Reply with quote

Kanchan,

Try this,
Code:
  SELECT Q,SUM(CLAIM) FROM (                                     
  SELECT EMPNAME, TS , CLAIM, 'Q1' AS Q FROM  SESSION.EMPTAB     
         WHERE MONTH(TS) <= 3                                     
  UNION ALL                                                       
  SELECT EMPNAME, TS , CLAIM, 'Q2' AS Q FROM  SESSION.EMPTAB     
         WHERE MONTH(TS) > 3 AND MONTH(TS) <= 6                   
  UNION ALL                                                       
  SELECT EMPNAME, TS , CLAIM, 'Q3' AS Q FROM  SESSION.EMPTAB     
         WHERE MONTH(TS) > 6 AND MONTH(TS) <= 9                   
  UNION ALL                                                       
  SELECT EMPNAME, TS , CLAIM, 'Q4' AS Q FROM  SESSION.EMPTAB     
         WHERE MONTH(TS) > 9 AND MONTH(TS) <= 12                 
) AS TAB                                                         
GROUP BY Q                 


Thanks You,
Sushanth
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Aug 12, 2010 12:55 pm    Post subject:
Reply with quote

sushanth bobby wrote:
Kanchan,

Try this,
Code:
  SELECT Q,SUM(CLAIM) FROM (                                     
  SELECT EMPNAME, TS , CLAIM, 'Q1' AS Q FROM  SESSION.EMPTAB     
         WHERE MONTH(TS) <= 3                                     
  UNION ALL                                                       
  SELECT EMPNAME, TS , CLAIM, 'Q2' AS Q FROM  SESSION.EMPTAB     
         WHERE MONTH(TS) > 3 AND MONTH(TS) <= 6                   
  UNION ALL                                                       
  SELECT EMPNAME, TS , CLAIM, 'Q3' AS Q FROM  SESSION.EMPTAB     
         WHERE MONTH(TS) > 6 AND MONTH(TS) <= 9                   
  UNION ALL                                                       
  SELECT EMPNAME, TS , CLAIM, 'Q4' AS Q FROM  SESSION.EMPTAB     
         WHERE MONTH(TS) > 9 AND MONTH(TS) <= 12                 
) AS TAB                                                         
GROUP BY Q                 


Thanks You,
Sushanth

besides disregarding different years (easily solved), You do realize that you probably have 4 tablescans ?

better would be to use year(ts) * 10 + int((month(ts) - 1) / 4 ) + 1 as Q
Back to top
View user's profile Send private message
sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1013
Location: India

PostPosted: Thu Aug 12, 2010 4:12 pm    Post subject:
Reply with quote

Nice Solution GuyC.

Sushanth
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Aug 12, 2010 4:34 pm    Post subject:
Reply with quote

ty,
just noticed it should be /3 instead of /4
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts find RACF group for access to spooled... jzhardy JCL & VSAM 1 Mon May 08, 2017 11:46 am
No new posts To find out size allocated to a seque... ashek15 JCL & VSAM 15 Thu Apr 27, 2017 9:42 am
No new posts find particular member name in PDS us... ravi243 CLIST & REXX 10 Mon Dec 19, 2016 6:44 pm
No new posts How to find the first monday of the w... abdulrafi COBOL Programming 10 Fri Nov 25, 2016 3:24 pm
No new posts Find out a active PGM jpsager JCL & VSAM 10 Fri Oct 07, 2016 4:32 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us