I am using SQL server 2014. I required the following report. HeadName is dynamic that can be any name if HeadType is earning then show Tot_hrs and Amount, if HeadType is the deduction, then show the only Amount.
Note: make sure that HeadName Data should not be hardcoded in the query because the user can add data in earning and deduction for .eg holiday work OT, incentive, Late deduction, Loan etc
I will provide two i.e fromPayslipDate and ToPayslipDate result should be displayed as the picture below.
CREATE TABLE #PaySlip (PaySlipID int, SlipNo varchar(50), SlipDate date, EmployeeID int, RatePerHrs numeric(18,2));
CREATE TABLE #PaySlipD (PaySlipID int,HeadType varchar(50),OrderNo int, HeadName varchar(50), Tot_hrs numeric(18,2), Amount numeric(18,2));
INSERT INTO #PaySlip
(PaySlipID,SlipNo,SlipDate ,EmployeeID,RatePerHrs)
values
(3, '2018-0001', '2018-10-03', 30, 7.00),
(4, '2018-0002', '2018-10-03', 23, 8.00);
INSERT INTO #PaySlipD
(PaySlipID,HeadType,OrderNo,HeadName,Tot_hrs,Amount)
values
( 3 , 'Earning' , 2 , 'Normal Hours' , 82 , 574 ),
( 3 , 'Earning' , 2 , 'OT Normal' , 13.57 , 0 ),
( 3 , 'Earning' , 3 , 'OT Holiday' , 0 , 0 ),
( 3 , 'Earning' , 5 , 'Incentive' , 0 , 0 ),
( 3 , 'Deduction' , 1 , 'Loan' , 0 , 0 ),
( 3 , 'Deduction' , 2 , 'Tax' , 0 , 26.13 ),
( 3 , 'Deduction' , 3 , 'Employee NPF' , 0 , 34.44 ),
( 3 , 'Deduction' , 4 , 'Employer NPF' , 0 , 48.22 ),
( 4 , 'Earning' , 1 , 'Normal Hours' , 71.24 , 498.68 ),
( 4 , 'Earning' , 2 , 'OT Normal' , 4.85 , 0 ),
( 4 , 'Earning' , 3 , 'OT Holiday' , 0 , 0 ),
( 4 , 'Earning' , 5 , 'Incentive' , 0 , 0 ),
( 4 , 'Deduction' , 1 , 'Loan' , 0 , 0 ),
( 4 , 'Deduction' , 2 , 'Tax' , 0 , 15.25 ),
( 4 , 'Deduction' , 3 , 'Employee NPF' , 0 , 29.92 ),
( 4 , 'Deduction' , 4 , 'Employer NPF' , 0 , 41.89 )
drop table #PaySlip
drop table #PaySlipD