Med Ada Med Ada - 7 months ago 19
SQL Question

Select the first log in Time using from Table using SQL Server 2008 R2

I have a table of employees that describe his last and first name, his Card ID and times he log in to an enterprise. I want to select the first time he logs in.

thise is a example of the table

thise is a example of the table

and this is the result that I want
this is the result that I want

Answer

You could group by the last name, first name and truncated date and return the minimal date per group. In modern SQL Server versions this truncation is pretty simple - you just cast the field_time to a date:

SELECT   last_name,
         first_name,
         CAST(field_time AS DATE) AS log_day,
         MIN(field_time) AS first_log
FROM     mytable
GROUP BY last_name, first_name, CAST(field_time AS DATE)