If you have such a field, "last_update", "create_date" or something like that I can help you write the query you need
Main Topics
Browse All TopicsI would to get last record entered for an employee with the status of 'ACTIVE'
For example:
select from employee_table where
status = 'ACTIVE' and department IN ('JEWELRY','HOUSEHOLD')
where emp_id = 'a value' and the rownum = last row entered.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You can add a column to the table which would indicate the order of entry. You would also need a trigger and sequence. The trigger can fetch the next value from the sequence and populate it. You might have to do some programming for existing records. In this way there would be no need to update the way records are getting inserted into the DB
After that based on the newly added column you can get the last entered record easily
i would compare something like max(rownum) = count(emp_id) so the max rownum = the total records for the particular id
select * from employee_table where
status = 'ACTIVE' and department IN ('JEWELRY','HOUSEHOLD')
where emp_id = 'a value'
and rownum = (select count(emp_id)
from employee_table et2
where status = 'ACTIVE' and department IN ('JEWELRY','HOUSEHOLD')
and et2.emp_id = emp_id)
Ok, here is the tested one, my apologies for not posting the complete solution before, yes rowum is a virtual value, if you put it as a column in a subquery then you can query on it:
select * from
(select colum1,colum2...,rownum as myrow
from employee_table where
status = 'ACTIVE' and department IN ('JEWELRY','HOUSEHOLD')
where emp_id = 'a value')
where myrow = (select count(emp_id)
from employee_table et2
where status = 'ACTIVE' and department IN ('JEWELRY','HOUSEHOLD')
and et2.emp_id = emp_id)
If you're going to select an arbitary row as "last" then you can eliminate the extra query and simply sort the data by the arbitrary rownumbers
and take the last one, simple way to do that is sort them descending order and then take the first one.
SELECT column1, column2
FROM ( SELECT *
FROM (SELECT column1, column2, ROWNUM AS myrow
FROM employee_table
WHERE status = 'ACTIVE'
AND department IN ('JEWELRY', 'HOUSEHOLD')
AND emp_id = 'a value')
ORDER BY myrow DESC)
WHERE ROWNUM = 1;
however.... doing the extra ordering by an arbitrary numbering scheme has no real value, nor does querying the rownumber that equals the count(*).
doing either of those is effectively picking a random row. In which case, it is best to just pick any one row.
SELECT column1, column2
FROM employee_table
WHERE status = 'ACTIVE'
AND department IN ('JEWELRY', 'HOUSEHOLD')
AND emp_id = 'a value'
AND ROWNUM = 1
If the assignment_status = 'ASSIGNED' then this is always the employee's current department. The department with 'ASSIGNED' status is the department that I will always want to return. For example, if an employee has two records, one with 'ASSIGNED assignment_status and one with 'PREVIOUS' assignment_status, I would like to return the 'ASSIGNED'.
In some cases, some employee have multiple records with assignment_status of 'PREVIOUS' for different departments and no 'ASSIGNED' status. In this case, I would like to return, the latest entry for the 'PREVIOUS' assignment_status.
In the attachment, rownum for the last one entered was always the highest rownum. Since it is a vitural column, I guess it is just a coincident that the entry that is the latest assignment_status was the highest rownum.
We will never have multiple rows with 'ASSIGNED' but it appears that there are multiple PREVIOUS with no ASSIGNED. I was hoping rownum would help but I will check to see if there is a date that exists. I'll get back with you in the A.M.
I am a novice when it come to PL/SQL.
In case there is a date that I could use. What syntax would I use for the following cases?
If status 'ASSIGNED' exists return department ( This one take precedent over all other status'). If this status exists, I don't want to process the other status)
Else if multiple statuses return the one with the latest date.
How would I incorporate into a PL/SQL statement?
select department from
(select department
from employee_table
where emp_id = 'a value'
and assignment_status in ('ASSIGNED','PREVIOUS')
order by assignment_status asc, assignment_date desc
)
where rownum = 1 --- here rownum has been determined reliably by the order by of the inner query
--- so the results will be coming in a determined order which gives rownum meaning.
I recommend split on
http:#24966603
http:#24991463
Business Accounts
Answer for Membership
by: sdstuberPosted on 2009-07-23 at 18:00:38ID: 24931625
Unless you have some field (usually populated by a trigger) that determines which row is the "last row" you can not do what you want.
There is no sorting of data by creation in Oracle.