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

Need help to write a DB2 query


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

New User


Joined: 08 Mar 2005
Posts: 34

PostPosted: Wed May 28, 2008 11:29 pm
Reply with quote

Could someone please help me to write a DB2 query.

There is a table A with fields
Entity, Type and Date.

Table A
----------------------------
| Entity |Type| Date |
----------------------------
| CAN | P | 05/01/2008 |
| USA | P | 05/05/2008 |
| FRA | C | 05/06/2008 |
| CAN | D | 05/07/2008 |
| CAN | C | 05/07/2008 |
| USA | D | 05/05/2008 |
| CAN | P | 05/05/2008 |
| USA | P | 05/02/2008 |
| FRA | P | 05/05/2008 |
|...
|...
|...
|...

Please help me to write a query to find:
Average daily volume of TYPE P totals for Entity CAN for a date range of 05/01/2008 to 05/07/2008.
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 May 29, 2008 12:03 am
Reply with quote

Hello,

You have not shown any "volume" column(s)? I would think you would want quantity or amount?
Back to top
View user's profile Send private message
rchandran_19

New User


Joined: 08 Mar 2005
Posts: 34

PostPosted: Thu May 29, 2008 2:47 am
Reply with quote

Hello Scherrer,

There is no column "volume" in this table.

For example if there are
total 10 'P' types for entity 'CAN' on 05/01/2008
total 20 'P' types for entity 'CAN' on 05/02/2008
total 30 'P' types for entity 'CAN' on 05/03/2008
total 40 'P' types for entity 'CAN' on 05/04/2008
total 50 'P' types for entity 'CAN' on 05/05/2008
total 60 'P' types for entity 'CAN' on 05/06/2008
total 70 'P' types for entity 'CAN' on 05/07/2008
i need the query to find the average of these totals (10+20+30+40+50+60+70)/7 = 40

Please help
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Thu May 29, 2008 3:17 pm
Reply with quote

Chandra,

Pls try this ..i have not tested this but hope this works ..

Code:


SELECT COUNT(ENTITY) / COUNT(DISTINCT DATE) AS AVERAGE
FROM  SESSION.EMP WHERE
ENTITY ='CAN' AND TYPE ='P'
AND DATE
BETWEEN '05/01/2008' AND '05/07/2008' GROUP BY ENTITY;

Back to top
View user's profile Send private message
Prajesh_v_p

Active User


Joined: 24 May 2006
Posts: 133
Location: India

PostPosted: Thu May 29, 2008 4:58 pm
Reply with quote

Hi rchandran & Ashimer,

Will the below query work as a Genereal solution to the problem?

rchandran, Can you test this out against ur table data?
Code:

SELECT  SUM(NOFP)/COUNT(DT1),ENTITY,TYPE FROM(                 
SELECT COUNT(1) AS NOFP,ENTITY,TYPE,COUNT(DISTINCT DATE) AS DT1
  FROM TBL.TEST2                                           
 GROUP BY  ENTITY,TYPE,DATE) AS TEMP                           
 GROUP BY ENTITY,TYPE                                           


Might need some tuning and tweeking!

Corrections are welcome

Thanks,
Prajesh
Back to top
View user's profile Send private message
rchandran_19

New User


Joined: 08 Mar 2005
Posts: 34

PostPosted: Fri May 30, 2008 6:50 pm
Reply with quote

I used Ashimer's query and it worked. Thank you very much Ashimer and Prajesh.
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 Compare 2 files(F1 & F2) and writ... JCL & VSAM 8
No new posts Write line by line from two files DFSORT/ICETOOL 7
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
Search our Forums:

Back to Top