Learn how to a build a cloud-first strategyRegister Now


Getting most uptodate record from DB2

Posted on 2012-09-15
Medium Priority
Last Modified: 2012-09-18
I tried a SQL of the following order but not sure if this is doing the correct way.

select *
from T
left join (select ID, max(A) from T) a
on T.ID=a.ID and T.A=a.A
left join (select ID, max(B) from T) B
on T.ID=b.ID and T.B=b.B
where T.X=1 and Y=2 and Z=3.
Question by:LuckyLucks
1 Comment
LVL 37

Accepted Solution

momi_sabag earned 2000 total points
ID: 38402111
it's not clear how you decide which record is the most updated one but assuming you can get it by doing an order by, for example order by a,b
then you need this

select *
from (select *, row_number() over(order by a desc, b desc) as rown from t) t
where rown=1

this way, the order by dictates the order of the records and by choosing rown=1 you choose the most updated one

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question