CREATE TEMPORARY TABLE `attedEmp`(
`employee_id` INT(11) NOT NULL,
`employee_code` VARCHAR(255),
`name` VARCHAR(255),
`designation_id` INT(11),
`designation` VARCHAR(255),
`present_days` INT(11),
`off_days` INT(11),
`ot_days` INT(11),
`total_days` INT(11)
) ENGINE = InnoDB;
INSERT
INTO
attedEmp
SELECT
`tbl_master_employee_details`.`id` AS `employee_id`,
`tbl_master_employee_details`.`employee_code`,
`tbl_master_employee_details`.`name`,
`tbl_master_employee_details`.`designation_id`,
`tbl_master_designations`.`designation`,
`tbl_manpower_attendance`.`present_days`,
`tbl_manpower_attendance`.`off_days`,
`tbl_manpower_attendance`.`ot_days`,
`tbl_manpower_attendance`.`total_days`
FROM
`tbl_manpower_attendance`
INNER JOIN
`tbl_master_employee_details` ON `tbl_master_employee_details`.`id` = tbl_manpower_attendance`.`employee_id
INNER JOIN
`tbl_master_designations` ON `tbl_master_designations`.`id` = `tbl_master_employee_details`.`designation_id`
WHERE
`client_site_id` = 3 AND `month_year` = '10-2018';
CREATE TEMPORARY TABLE `EmpSalary`(
`employee_id` INT(11) NOT NULL,
`salary_head_id` INT(11),
`salary_head` VARCHAR(255),
`Amount` FLOAT(8, 2)
) ENGINE = InnoDB;
INSERT
INTO
EmpSalary
SELECT
em.employee_id,
mp.salary_head_id,
sh.salary_head,
mp.amount
FROM
attedEmp em
INNER JOIN
tbl_client_contracts cc ON em.designation_id = cc.designation_id
INNER JOIN
tbl_salary_templates st ON st.id = cc.salary_template_id
INNER JOIN
tbl_mapping_salary_template_salary_heads mp ON st.id = mp.salary_template_id
INNER JOIN
tbl_salary_heads sh ON mp.salary_head_id = sh.id
WHERE
cc.client_site_id = 4;
/*
SELECT EmpSalary.*, attedEmp.* From EmpSalary LEFT JOIN attedEmp On attedEmp.employee_id = EmpSalary.employee_id;
SET
@PivotColumnNames = '',
@PivotSelectColumnNames = '';
SELECT
@PivotColumnNames :=(
GROUP_CONCAT(DISTINCT((salary_head)))
)
FROM
EmpSalary;
SELECT
@PivotColumnNames;
SELECT
(
CONCAT(
'employee_id, name, employee_code, designation, present_days, off_days, ot_days,total_days,',
@PivotColumnNames
)
);
SET
@DynamicPivotQuery = CONCAT(
SET
@PivotColumnNames = '',
@PivotSelectColumnNames = '';
SELECT
@PivotColumnNames :=(
GROUP_CONCAT(DISTINCT((salary_head)))
)
FROM
EmpSalary;
SELECT
@PivotColumnNames;
SELECT
(
CONCAT(
'employee_id, name, employee_code, designation, present_days, off_days, ot_days,total_days,',
@PivotColumnNames
)
);
SET
@DynamicPivotQuery = CONCAT(
'SELECT employee_id, name, employee_code, designation, present_days, off_days, ot_days,total_days,',
@PivotColumnNames,
'
FROM EmpSalary es INNER JOIN attedEmp em ON es.employee_id=em.employee_id as
pivot(sum(amount) for salary_head in (',
@PivotColumnNames,
')) as pvt ORDER BY name'
);
SELECT
@DynamicPivotQuery;