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.
Dig Deeper on Oracle and SQL
Related Q&A from Rudy Limeback
Read an example of an SQL case expression from our SQL expert Rudy Limeback. Continue Reading
Read SQL expert Rudy Limeback's advice for counting combinations in a table with SQL's GROUP BY clause Continue Reading
Read about the Mimer Validator, a tool used to verify your SQL code, in this tip from SQL expert Rudy Limeback. Continue Reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.