How to get the proper Time
Using SQL Server
Table1
ID Actual_Intime Actual_Outtime
001 08:00:00 18:00:00 (HHMMSS)
002 20:00:00 06:00:00
003 05:00:00 14:00:00
004 19:30:00 03:30:00
...
...
Actual_Intime Datatype is nvarchar
Actual_Outtime Datatype is nvarchar
I need to validate the employee time like
"If outtime is less than Intime, then it should take the intime and outtime on the same day"
"if outtime is greater than intime, then it should take the intime on the same day and outtime should be the next day.
Table2
ID Date Time
001 20100102 06:00:00
001 20100102 08:00:00
001 20100102 14:00:00
001 20100102 19:00:00
002 20100102 19:00:00
002 20100102 21:00:00
002 20100103 03:00:00
002 20100103 07:00:00
...,
...
Date Datatype is nvarchar
Time Datetype is nvarchar
Date Format: yyyymmdd
Time Format: HHMMSS
I need to take the Min(time) and Max(time) from the table2 but table2 compare the actual intime and actual outtime from table1
Expected Output
ID Date Intime Outtime
001 20100102 06:00:00 19:00:00
(Here Actual_Outtime is less than Actual_Intime from 开发者_如何学运维table1, so In table2 it should take the intime and outtime on the same day)
002 20100102 19:00:00 07:00:00
(Here Actual_Outtime is greater than Actual_Intime from table1, so In table2 it should take the next day time as outtime)
How to get the Intime and Outtime compare with actual Intime and actual Outtime from Table1.
Need Query Help
Not sure whether Table1
is actually needed here. I understand, Table2
contains all the necessary information to generate the result table.
SELECT
ID,
date = CONVERT(char(8), intime, 112),
intime = CONVERT(char(8), intime, 108),
outtime = CONVERT(char(8), outtime, 108)
FROM (
SELECT
ID,
intime = MIN(dt),
outtime = MAX(dt)
FROM (
SELECT
ID,
dt = CAST(Actual_Intime + ' ' + Actual_Outtime AS datetime)
FROM Table2
) s
) m
I assume here that Table1.Actual_Intime
corresponds to MIN(CAST(Table2.Date + ' ' + Table2.Time AS datetime))
, and Table1.Actual_Outtime
to MAX(...)
, which seems to agree with the description given.
精彩评论