?
Solved

How can I select data with the largest version number?

Posted on 2008-10-03
15
Medium Priority
?
336 Views
Last Modified: 2013-12-19
Hi I have this code here but something is wrong:

select a.Title
  from t_ABC a
 where a.version = Max(a.version);

The table contains multiple entries with the same title, having a different version number. Is it possible to select the ones with the greatest version for each Title?

Thanks.
0
Comment
Question by:Rassac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
  • +2
15 Comments
 
LVL 11

Expert Comment

by:aaronakin
ID: 22634134
select a.Title, MAX(a.version)
  from t_ABC a
  group by a.Title
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 22634145
select title from (
select title from t_abc
order by version desc
)
where rownum = 1
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 22634165
ooops, ignore my previous post, that was to get the title for highest overall version
not version per title.

select * from
(
select t_abc.*, row_number() over(partition by title order by version desc) rn from t_abc
)
where rn = 1
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Rassac
ID: 22634197
The problem is that I am using a nested query and cannot have two columns. Is there any other way please?

thanks.
0
 
LVL 11

Expert Comment

by:aaronakin
ID: 22634210
SELECT Title
  FROM
    (select a.Title, MAX(a.version)
  from t_ABC a
  group by a.Title
    )b
0
 
LVL 11

Expert Comment

by:aaronakin
ID: 22634227
If you want just the version number...

SELECT Version
  FROM
    (select a.Title, MAX(a.version) AS Version
  from t_ABC a
  group by a.Title
    )b
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1000 total points
ID: 22634241
I am not an Oracle guru, so forgive me -- hopefully you get the idea.
-- use IN statement
select a.Title
  from t_ABC a
 where a.version IN (select Max(version) from t_ABC b where b.Title = a.Title);
 
-- use INNER join
select a.Title
  from t_ABC a INNER JOIN 
(select Title, Max(version) As version from t_ABC) b 
ON b.Title = a.Title AND b.version = a.version;

Open in new window

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22634249
"The problem is that I am using a nested query and cannot have two columns. Is there any other way please?"

You can't do it with a nested query.  You have to use a derived table.

ex:

select a.Title
  from t_ABC a
  join (select title,max(version) maxVersion from t_ABC group by Title)b
on a.title=b.title
and a.version = b.maxversion

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 22634251
If you only want the title and no other columns, then who cares about the version...


select distinct title from t_abc
0
 
LVL 11

Expert Comment

by:aaronakin
ID: 22634270
Maybe it would be better to provide us with the fully query so we know the context of how it's being used.

Also, is this for SQL Server or Oracle?  What version?
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22634275
That way, you can have other columns in the select portion as well.  Since this is a version, I would suspect only one row can have a version, so either method should work.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22634290
Oracle 8.x in zones it looks like, but yes please confirm.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 22634308
aaronakin,  I believe it's Oracle and version 8, based on the zones the query was posted to.

Rassac,  if none of the above options work please post some sample data and expected output
there are multiple ways to interpret what it is you are requesting.
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22634317
Rassac, for my second example (http:#22634241), I was trying to suggest same as Brandon in comment following mine.  I left out the Group By Title, so if you liked second method better look at Brandon's comment - http:#22634249.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 22634412
can version be NULL?

if not, then the query in 22634249 is just a complicated (slower) way to write

select distinct title from t_abc

if version can be NULL, then how do you want those nulls processed?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Suggested Courses

801 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