我想在两个表EmployeeDetails
和MachineAttendance
之间的CTE中使用联接查询,但我很困惑-如何使用它?
;With CTE As
(
select ed.EmpName
from EmployeeDetails ed
)
,cte1; AS
(
select
ma.EmpID, cast(MA.Datetime as Date) as [Date],
case when MA.INOUT = 1 then DateTime end as INOUT_INTIME,
case when MA.INOUT = 2 then DateTime end as INOUT_OUTTIME
from
MachineAttendance MA
), cte2 as
(
select
EmpID, [Date],
MAX(INOUT_INTIME) AS INTIME,
MAX(INOUT_OUTTIME) AS OUTTIME,
DATEDIFF(Hour, MAX(INOUT_INTIME), MAX(INOUT_OUTTIME)) as [Hours]
from
CTE1
group by
EmpID, [Date]
)
select
EmpID, [Date], INTIME, OUTTIME, [Hours],
case
when [Hours] >= 8 then 1
when [Hours] = 0 then 0
when [Hours] >= 6 then 0.5
end as [Day],
case
when [Hours] > 8 then [Hours] - 8
else 0
end as OT,
case
when [Hours] >= 8
then ([Hours] - 8) * 100 else 0
end as OTAMount,
Convert(varchar(10), Date, 120) as [Date],
Convert(varchar(10), INTIME, 108) as [Time],
Case
When Convert(Time, INTIME, 108) > '09:10'
Then 1 else 0
end as Late
from
cte2
inner join
cte On cte.EmpID = cte2.EmpID`
答案 0 :(得分:0)
您输入了mysql和sql server的混合语法,因此我认为下面的命令会有用一些逗号
;With CTE As
(
select ed.EmpName from EmployeeDetails ed
)
,cte1 AS
(
SELECT ma.EmpID, CAST(MA.Datetime as Date) AS [Date],
CASE WHEN MA.INOUT = 1 THEN DateTime END AS INOUT_INTIME,
CASE WHEN MA.INOUT = 2 THEN DateTime END AS INOUT_OUTTIME
From
MachineAttendance MA
),
cte2
as
(
select EmpID, [Date], MAX(INOUT_INTIME) AS INTIME,
MAX(INOUT_OUTTIME) AS OUTTIME
, DATEDIFF(Hour, MAX(INOUT_INTIME), MAX(INOUT_OUTTIME)) as [Hours]
FROM CTE1
GROUP BY EmpID, [Date]
)
select EmpID, [Date], INTIME, OUTTIME, [Hours]
, CASE WHEN [Hours] >= 8 THEN 1
WHEN [Hours] = 0 THEN 0
WHEN [Hours] >= 6 THEN 0.5 END AS [Day],
CASE WHEN [Hours] > 8 then [Hours] - 8 else 0 End as OT,
CASE WHEN [Hours] >= 8
then ([Hours] - 8) * 100 else 0 END AS OTAMount,
Convert(varchar(10),Date,120) as [Date],
Convert(varchar(10),INTIME,108) as [Time],
Case When Convert(Time,INTIME,108) > '09:10' Then 1 else 0 end as Late
from cte2
INNER Join cte On cte.EmpID=cte2.EmpID