Click here to Skip to main content
15,884,047 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi. I am having difficulty getting the last/latest record from table.

These are the structure of the tables

[TABLE RESULT]

I need to place it in the subquery.
SQL
select EmployeeNo
,(Select PosCode from EmploymentHistory where EmployeeNo=EmpPersonalInfo.EmployeeNo) as PositionCode
,(Select Rate from EmployeeSalaryHistory where EmployeeCode=EmpPersonalInfo.EmployeeNo) as PositionCode
from  EmpPersonalInfo where EmployeeCode=902575



I can distinguish the latest record in both records if Status=1, what if status=0 for all the records in the table, how can I get the latest record (it depends on the effectivitydatefrom- for EmploymentHistory table, and EffectivityDate -for EmployeeSalaryHistory)

Aside from getting TOP 1 and ORDER BY DESC, is there anyway to do this in most suggested way?

What I have tried:

I tried the codes above but i am seeking for additional information.

AND I TRIED THIS, but i cant build the logic, can I manipulate it here?

select EmpPersonalInfo.LastName,EmpPersonalInfo.FirstName,EmpPersonalInfo.MiddleName,EmpPersonalInfo.MI,
EmploymentInfo.Street,EmploymentInfo.District,EmploymentInfo.City,EmploymentInfo.Country,EmploymentInfo.ZipCode
,position.PosTitle,tblEmployeeMainInfo.BranchCode,EmpDepartment.Department,tblEmployeeMainInfo.DivisionCode
,tblEmployeeMainInfo.AssignmentCode,EmploymentInfo.HireDate
,EmployeeSalaryHistory.EffectivityDate,EmployeeSalaryHistory.Rate
,EmployeeSalaryHistory.RateType,EmployeeSalaryHistory.Active as SalaryStatus,
EmploymentHistory.EffectivityDateFrom,EmploymentHistory.EffectivityDateTo,
EmploymentHistory.Status as EmploymentStatus
from EmpPersonalInfo LEFT JOIN EmploymentHistory ON EmpPersonalInfo.EmployeeNo=EmploymentHistory.EmployeeNo
LEFT JOIN EmployeeSalaryHistory ON EmpPersonalInfo.EmployeeNo = EmployeeSalaryHistory.EmployeeCode
LEFT JOIN tblEmployeeMainInfo ON tblEmployeeMainInfo.EmployeeCode=EmpPersonalInfo.EmployeeNo
LEFT JOIN EmploymentInfo on EmpPersonalInfo.EmployeeNo=EmploymentInfo.EmployeeNo
LEFT JOIN Position ON Position.PoCode = EmploymentHistory.PosCode
LEFT JOIN EmpDepartment on EmpDepartment.DeptID = EmploymentHistory.DepartmentID
where EmpPersonalInfo.EmployeeNo=902575
Posted
Updated 16-Feb-16 16:11pm
v2

1 solution

There is a way, you can asssign a sequence number using a ranking functions and then filter on that:
SQL
with EmploymentHistoryEx AS
(
    Select EmployeeNo, PosCode, Status, EffectivityDateFrom, EffectivityDateTo
        row_number() over(partition by EmployeeNo order by Status desc, EffectivityDateFrom desc) as HistorySeq
    from EmploymentHistory
)

You do the same for the other table. Then in your main query you join to the first record:
SQL
select 
    ...
    EmploymentHistoryEx.EffectivityDateFrom,
    EmploymentHistoryEx.EffectivityDateTo,
    EmploymentHistoryEx.Status as EmploymentStatus
    
from 
    EmpPersonalInfo 
    LEFT JOIN EmploymentHistoryEx 
        ON EmpPersonalInfo.EmployeeNo=EmploymentHistoryEx.EmployeeNo
        AND EmploymentHistoryEx.HistorySeq=1 
    ...
where EmpPersonalInfo.EmployeeNo=902575
 
Share this answer
 
v2
Comments
bjay tiamsic 17-Feb-16 19:51pm    
Hi. Thank you for this. It works, but not when status of any of the records is set to true. I tried the old record to set the status to true and it did not get that value. It is supposed to be display, in other words, STATUS field is my priority in checking and if no record has STATS=TRUE then the latest record based on effectivitydatefrom (EmploymentHistory table) and EffectivityDate (EmployeeSalaryHistory) should be considered
Tomas Takac 19-Feb-16 2:31am    
You can do that in row_number(), just sort first by status then by date. I updated my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900