Home      Mainframe Forum      Mainfarme Tutorials      IBM Manuals      Mainframe Interview Questions      Mainframe Books      IT News     SiteMap     Downloads


     
 
MAINFRAME - TIP OF THE DAY : All available storage is assigned to the job or jobstep, If REGION is coded 0K or 0M

Google
 
Web mainframegurukul.com

Is there a better(more efficient) way to write this query?

 
Post new topic   Reply to topic    mainframegurukul.com Forum Index -> DB2 SQL - DB2 PROGRAMMING
  View previous topic :: View next topic  
Author Message
wandilly
Member


Joined: 07 Jun 2012
Posts: 1

Answer was approved by Question owner.
Useful Post
0 votes
Not an useful post
Greate Post!! Salute Author
0 salutes

PostPosted: Wed Jun 17, 2015 3:07 am    Post subject: Is there a better(more efficient) way to write this query? Reply with quote

I was told to provide summary counts by month from two tables
the rows are added to table1 first(current month) then content of monthly
is saved in TABLE2
Hence both tables are identical, and date field record every change down to the microsecond (AUD_INSERT_TS) and the source of the change (if LAST_UPDATE_USER is “...BTCH”, it was a Batch change, otherwise it was Online). But unfortunately that only works for Inserts and Updates (UPDATE_FUNC_IND = ‘I’ or ‘U’). For Deletes (UPDATE_FUNC_IND = ‘D’), the LAST_UPDATE_USER element is actually the value before the change. So the logic below uses LAST_UPDATE_USER only for Insert or Update changes; but if UPDATE_FUNC_IND = ‘D’, it assumes it is a Batch change if it occurred before 6 a.m., otherwise it was an Online change.

TABLE1 (current month)
(current) Colum name Data description
COL1 AUD_INSERT_TS Date that the row was created/modified
COL2 LAST_UPDATE_USER transaction via Batch Process or Online
COL3 UPDATE_FUNC_IND
and
TABLE2 (historic - all other past months)
(current) Colum name Data description
COL1 AUD_INSERT_TS Date that the row was created/modified
COL2 LAST_UPDATE_USER transaction via Batch Process or Online
COL3 UPDATE_FUNC_IND

But I am not sure if there is a better way to write the DB2 query below:

SELECT MONTH(A.AUD_INSERT_TS) AS MTH_YR,
SUM(CASE WHEN (A.LAST_UPDATE_USER LIKE '%BTCH' AND
A.UPDATE_FUNC_IND IN ('I','U'))
OR (HOUR(TIME(A.AUD_INSERT_TS)) < 6 AND
A.UPDATE_FUNC_IND = 'D')
THEN 1 ELSE 0 END) AS CT_BTCH,
SUM(CASE WHEN (A.LAST_UPDATE_USER NOT LIKE '%BTCH' AND
A.UPDATE_FUNC_IND IN ('I','U'))
OR (HOUR(TIME(A.AUD_INSERT_TS)) >= 6 AND
A.UPDATE_FUNC_IND = 'D')
THEN 1 ELSE 0 END) AS CT_ONLN,
COUNT(*) AS CT_ALL
FROM (SELECT * FROM TABLE2 UNION
SELECT * FROM TABLE1) A
WHERE YEAR(A.AUD_INSERT_TS) = 2015
GROUP BY MONTH(A.AUD_INSERT_TS);

MTH_YR CT_BTCH CT_ONLN CT_ALL

1 548997 7052 556049
2 507036 5684 512720
3 561471 7019 568490
4 526635 6568 533203
5 530777 6078 536855
6 80378 2549 82927
Back to top
View user's profile Send private message
Click here, If this post answer your question. <-- Click on right mark icon. If this post answer your question.

Display posts from previous:   
Post new topic   Reply to topic    mainframegurukul.com Forum Index -> DB2 SQL - DB2 PROGRAMMING 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

Related topics
 Topics   Replies   Author   Views   Last Post 
No new posts Query For IEBPTPCH 3 padmajeet.gaikwad 13445 Mon Feb 01, 2016 10:56 pm
academyindia4 View latest post
No new posts Unable to achieve below scenario using ICETOOL 27 sirianangel 33683 Mon Feb 01, 2016 10:01 pm
academyindia4 View latest post
No new posts Need to put records a Sorting order as mentioned below. 14 Yash1911 12634 Mon Jan 25, 2016 9:50 pm
academyindia4 View latest post
No new posts how to deal with the below scenario using iebgener 7 mainframes_6002 11170 Wed Dec 07, 2011 10:34 am
DikDude View latest post
No new posts Coulu someone explain the parameters in the below code? 1 lovely sweety 11352 Thu Jun 09, 2011 3:50 pm
dbzTHEdinosauer View latest post
 


Cobol Tutorial
This cobol tutorial covers most of the important topics like STRING, UNSTRING, COMP, COMP-3.....
DB2 Tutorial
DB2 Tutorial focuses on DB2 COBOL Programming. Explains in simple language. Some Chapters are locked, Forum members have free access to these chapters
CICS Tutorial
This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming...
JCL Tutorial
This is most popular JCL tutorial from mainframegurukul. It does contain important jcl ....
SORT Tutorial
This Tutorial covers all important aspects of DFSORT. Has more SORT examples
INTERVIEW Questions
This page covers important interview questions
Mainframe Jobs
Mainframe Jobs posted by members of mainframegurukul forum
FaceBook Page
MainframeGurukul FaceBook Page
LinkedIn Page
MainframeGurkul Linkedin Page

This widget requires Flash Player 9 or better







Go to top of the page
 

Online ABEND Reference ||  JCL References ||  COBOL References ||  VSAM References ||  Tutorials by Drona Series ||  SQL tutorial ||  BOOKS  ||  DB2 INTERVIEW QUESTIONS ||  COBOL INTERVIEW QUESTIONS  ||  JCL INTERVIEW QUESTIONS ||  JCL2 INTERVIEW QUESTIONS ||  VSAM INTERVIEW QUESTIONS ||  CICS INTERVIEW QUESTIONS  ||  Online tutorials ||  Online ABEND Reference ||  JCL References ||  COBOL References ||  VSAM References ||  Tutorials by Drona Series ||  SQL tutorial ||  BOOKS  ||  SiteMap  ||  Expeditor Tutorial  ||  FILE-AID Tutorial  ||  Changeman Tutorial  ||  COBOL   ||  DB2   ||  JCL  ||  CICS  ||  VSAM  ||  DB2 Interview Questions ( 110 )   || Simple JCL Tutorials  || JCL Tutorial from MainframeGurukul.com   || Simple JCL Tutorial - Chapter1 ;|| Mainframe Forum - Tutorials  || Mainframe Tutorials || Mainframe CICS tutorial|| Mainframe COBOL Tutorial

Drona Educational Forums - Mainframe Cobol DB2 CICS Board
Powered by phpBB

mainframetopic-7885.html