Q
Problem solve Get help with specific problems with your technologies, process and projects.

Latest version of each jobcode

I am trying to retrieve information from a single table. The table contains job descriptions, about 500,000 rows. Each description has multiple rows. My subqueries work separately, but not together. Do you have any suggestions?

I am trying to retrieve information from a single table. The table contains job descriptions, about 500,000 rows....

Each description has multiple rows (30-50/desc). I am retrieving the jobcode (varchar2) and version (date). I want the maximum version (most recent date) and from the max versions I want only the codes that have a subcatvalue equal to some text. Here's what I was thinking but it doesn't seem to work.

select distinct a.jobcode, a.version
from bainq.hr_jobdescription_new a
where ((a.jobcode, a.version) in 
(select distinct b.jobcode, b.version
from bainq.hr_jobdescription_new b 
where b.subcatvalue = 'REGISTRATION LICENSE CERTIFICATES')) 
and ((a.jobcode, a.version) in 
(select c.jobcode, max(c.version) 
from bainq.hr_jobdescription_new c 
group by c.jobcode))

The subqueries work separately, but not together. Do you have any suggestions? Thanks in advance.

Your description of what you want is pretty easy to understand, and this is the solution that suggests itself:

select a.jobcode
     , a.version
  from bainq.hr_jobdescription_new a
 where a.version 
     = ( select max(version)
           from bainq.hr_jobdescription_new  
          where jobcode = a.jobcode )
   and a.subcatvalue 
     = 'REGISTRATION LICENSE CERTIFICATES'

The subquery selects only the latest version for each jobcode. It's a correlated subquery and so it has the same effect as grouping. Then the second condition in the WHERE clause eliminates those max versions that aren't the specified subcatvalue.

The DISTINCT is not necessary because presumably each jobcode has only one latest version.

This was last published in August 2005

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close