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
 

 

Fetch the latest record from a table thru an SQL query

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

New User


Joined: 27 Feb 2008
Posts: 72
Location: india

PostPosted: Tue Dec 01, 2015 7:13 pm    Post subject: Fetch the latest record from a table thru an SQL query
Reply with quote

Hi,

I want to fetch the latest record from a DB2 table based on one field PO create time stamp, thru an SQL query only.

Thanks
Subrat
Back to top
View user's profile Send private message

Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 1722
Location: UK

PostPosted: Tue Dec 01, 2015 8:40 pm    Post subject:
Reply with quote

If it by an SQL query only why did you post a request for a sort solution in the sort forum?
Possibly:
Code:
Where TIMESTAMP = Max(Timestamp)

but you will have to do your due diligence in checking that.
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1623
Location: NY,USA

PostPosted: Tue Dec 01, 2015 9:18 pm    Post subject:
Reply with quote

or you can say..
Code:
Order by PO create time stamp DESC
fetch first 1 row only
Back to top
View user's profile Send private message
maxsubrat

New User


Joined: 27 Feb 2008
Posts: 72
Location: india

PostPosted: Wed Dec 02, 2015 11:10 am    Post subject: Reply to: Fetch the latest record from a table thru an SQL query
Reply with quote

Suppose my table structure is like below:

ACT-ID PO-NBR ACT-TS
12 12345 2015-12-02-10-00-00-000000
12 12345 2015-12-02-09-00-00-000000
12 12345 2015-12-02-08-00-00-000000
15 99999 2015-12-01-04-00-00-000000
15 99999 2015-12-01-09-00-00-000000
25 12345 2015-12-03-09-00-00-000000
25 88888 2015-12-01-09-01-00-000000

I want to fetch the latest ACT-TS for each PO-NBR and ACT-ID in (12,15) only.
so my output data should be:

ACT-ID PO-NBR ACT-TS
12 12345 2015-12-02-10-00-00-000000
15 99999 2015-12-01-09-00-00-000000
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1623
Location: NY,USA

PostPosted: Wed Dec 02, 2015 11:24 am    Post subject:
Reply with quote

Code:
Select acct-Id,po-nbr,max(act-to)
From table
Group by acc-Id,po-nbr
Order by 1,2
Back to top
View user's profile Send private message
maxsubrat

New User


Joined: 27 Feb 2008
Posts: 72
Location: india

PostPosted: Wed Dec 02, 2015 11:35 am    Post subject: Reply to: Fetch the latest record from a table thru an SQL query
Reply with quote

suppose I have this type of record structure:

ACT-ID PO-NBR ACT-TS
02 12222 2015-12-12-10-00-00-000000
02 12222 2015-12-15-10-00-00-000000
10 19222 2015-12-12-10-00-00-000000
12 12345 2015-12-02-10-00-00-000000
12 12345 2015-12-02-09-00-00-000000
12 12345 2015-12-02-08-00-00-000000
15 99999 2015-12-01-04-00-00-000000
15 99999 2015-12-01-09-00-00-000000
25 12345 2015-12-03-09-00-00-000000
25 88888 2015-12-01-09-01-00-000000
Back to top
View user's profile Send private message
Rohit Umarjikar

Senior Member


Joined: 21 Sep 2010
Posts: 1623
Location: NY,USA

PostPosted: Wed Dec 02, 2015 11:38 am    Post subject:
Reply with quote

Have tried first , what has given to you? Are you clear what would you want to achieve ?
If you want further help then provide us sample input, logic , output what yo want.

Add a Where condition to above sql,
Code:
Where act-id in  (12,15)
Back to top
View user's profile Send private message
maxsubrat

New User


Joined: 27 Feb 2008
Posts: 72
Location: india

PostPosted: Wed Dec 02, 2015 3:45 pm    Post subject: Reply to: Fetch the latest record from a table thru an SQL query
Reply with quote

Thanks
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 1722
Location: UK

PostPosted: Wed Dec 02, 2015 7:38 pm    Post subject:
Reply with quote

And PLEASE use the code tags when presenting data.
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 Join in SQL Query vickey_dw DB2 1 Tue Jan 17, 2017 12:15 am
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm


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