How to use Join query between CTE and two table.? : I want to use a join query in cte between two table Employee Details and second one is Machine Attendance,but getting confuse how it will use.?

        ;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`

