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
 

 

Get all the records with latest timestamp & Approved sta

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

Moderator


Joined: 14 Oct 2005
Posts: 1187
Location: Bangalore,India

PostPosted: Wed Dec 02, 2009 11:22 am    Post subject: Get all the records with latest timestamp & Approved sta
Reply with quote

Hi All,

I have a table ACC1 which contain primary key as ACC_number & UPDT_DT_TM.

Now I want to fetch all the latest records for Account_number where latest record timestamp is older than 60 days & they are ina ctive status

Code:
SELECT ACC_number, MAX(UPDT_DT_TM),status, colb,colC
FROM TB
GROUP BY  ACC_number
having Status ='approved'
and  Days(UPDT_DT_TM)-Days(current_timestamp)>60
WITH UR


I dont have access to db2 currently. Would you please provide optimized query for above criteria
Back to top
View user's profile Send private message

Srihari Gonugunta

Active User


Joined: 14 Sep 2007
Posts: 295
Location: Singapore

PostPosted: Wed Dec 02, 2009 12:11 pm    Post subject:
Reply with quote

Ekta,
You can try this.

Code:
SELECT ACC_number, MAX(UPDT_DT_TM)
FROM TB
WHERE Status ='approved'
and  DAYS(CURRENT TIMESTAMP) - Days(UPDT_DT_TM) > 60
GROUP BY ACC_number;
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Wed Dec 02, 2009 1:49 pm    Post subject:
Reply with quote

I'm not sure of the requirement, but if I understand it correctly
then both solutions are wrong.

What the previous sqls return : you're searching for statusses that are 60 days old and of those you want the most recent one.

How I understand it :
You're only interested in the last status of account_number and that one has to be 60 days old and .

some guidelines for performance :
- avoid "having"
- avoid where-clause on function(column) or on calculations involving columns.

Code:
Select * from TB A
where a.updt_dt_tm = (select max(b.updt_dt_tm) from TB b
                       where a.ACC_number = b.ACC_number)
  and a.updt_dt_tm < timestamp(current_date - 60 days,'00.00.00')
  and A.status = 'approved'
Back to top
View user's profile Send private message
guptae

Moderator


Joined: 14 Oct 2005
Posts: 1187
Location: Bangalore,India

PostPosted: Wed Dec 02, 2009 1:51 pm    Post subject:
Reply with quote

Hello Guyc,

Thanks Alot.I Have not checked the solution yet but it seems to be as per my requirement.
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 Check if any Detail records and extra... V S Amarendra Reddy SYNCSORT 19 Mon May 08, 2017 8:54 pm
No new posts Join giving more records than expected Danielle.Filteau SYNCSORT 2 Thu Mar 09, 2017 3:28 am
No new posts TSO or command line utility to genera... kishpra JCL & VSAM 3 Thu Mar 09, 2017 1:11 am
No new posts Extract set of records matching on ac... bhaskar_kanteti DFSORT/ICETOOL 3 Mon Mar 06, 2017 7:19 am
No new posts Inserting records based on conditions vickey_dw DFSORT/ICETOOL 9 Wed Feb 22, 2017 1:33 pm


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