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

need to find sum of quarterly claims?


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
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
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
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

Moderator Emeritus


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

PostPosted: Thu Aug 12, 2010 12:24 am
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: 1020
Location: India

PostPosted: Thu Aug 12, 2010 7:38 am
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
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: 1020
Location: India

PostPosted: Thu Aug 12, 2010 4:12 pm
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
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts To find whether record count are true... DFSORT/ICETOOL 6
No new posts Find the size of a PS file before rea... COBOL Programming 13
No new posts Find the occurrence of Key Field (Par... DFSORT/ICETOOL 6
No new posts Find a record count/numeric is multip... COBOL Programming 1
No new posts Need to find a specific STRING COBOL Programming 11
Search our Forums:

Back to Top