Skip to content
Advertisement

i can’t Merge two tables

i have one tables attendance main table i created 2 views from attendance table attend view and leaved view view:attend

select `dbtest`.`employee`.`empId` AS `empId`,`dbtest`.`attendance`.`timeAttendnace` AS `timeAttendnace` from (`dbtest`.`employee` left join `dbtest`.`attendance` on(`dbtest`.`attendance`.`empId` = `dbtest`.`employee`.`empId` and `dbtest`.`attendance`.`statusAttendnace` = 'In'))

view:leaved

select `dbtest`.`employee`.`empId` AS `empId`,`dbtest`.`attendance`.`timeAttendnace` AS `timeAttendnace` from (`dbtest`.`employee` left join `dbtest`.`attendance` on(`dbtest`.`attendance`.`empId` = `dbtest`.`employee`.`empId` and `dbtest`.`attendance`.`statusAttendnace` = 'Out'))

after that i used this code to merge attend and leaved view

select `attend`.`empId` AS `empId`,`attend`.`timeAttendnace` AS `clockIn`,`leaved`.`timeAttendnace` AS `clockOut` from (`dbtest`.`attend` join `dbtest`.`leaved` on(`attend`.`empId` = `leaved`.`empId`)) group by `attend`.`timeAttendnace` order by `attend`.`empId`

the result was like this result

i need each employee has attend time and leaved time in same day

Advertisement

Answer

This following query will return you employee wise daily In and Out Time in a single row.

SELECT 
cast(TimeAttendence AS DATE),
empid,
MIN(CASE WHEN StatusAttendence = 'In' THEN TimeAttendence END) InTime,
MAX(CASE WHEN StatusAttendence = 'Out' THEN TimeAttendence END) OutTime
FROM main_table
GROUP BY cast(TimeAttendence AS DATE),empid
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement