-2

I am using SQL Server. I have two tables employees and employees entry times.

I am joining these tables using a LEFT OUTER JOIN. But I want to get the last input value for each employee(Last entered date).

Summary purpose

  1. Select all employee. If there is no input value corresponding to employee return null.
  2. If there are much input value corresponding, select last entered date

How do I do this?

The problem I'm having is exponentially more complex.

So simplified example for understanding;

Table 1 : Employees

Id
Name

Table 2 : Employee_Entry_Times

Id
Time_Entered
Employee_REFER
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Well - show us your query that you already have! And show us sample data, what your query returns currently, and what you *want* to return instead... – marc_s Feb 12 '22 at 16:20
  • "How to select unique columns for two joined tables" => Sounds like this is what you are chasing after : Select Union https://www.w3schools.com/sql/sql_union.asp Unless you update your question to be more specifc. – N Subedi Feb 12 '22 at 16:23
  • You can use either `TOP (1)` in an `APPLY`, or `ROW_NUMBER` – Charlieface Feb 12 '22 at 21:02

1 Answers1

1

I think this should work.

select e.name, MAX(t.Time_Entered)
from 
employees e left outer join Employee_Entry_Times t
on e.id = t.Employee_REFER
group by e.name
Pontnou
  • 96
  • 1
  • 4