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: 1278
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 Removing Duplicates based on certain ... chandracdac DFSORT/ICETOOL 8 Fri Dec 09, 2016 4:40 am
No new posts Limit duplicate records in the SORT pshongal SYNCSORT 6 Mon Nov 21, 2016 12:54 pm
No new posts How to split the records using the am... vnktrrd DFSORT/ICETOOL 24 Fri Oct 28, 2016 7:33 pm
No new posts Sort records based on numeric field. Alks SYNCSORT 2 Wed Oct 19, 2016 10:14 pm
No new posts abend sort based on count records in ... anatol DFSORT/ICETOOL 5 Mon Oct 17, 2016 10:10 pm


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