I am trying to shift SQL records to single line so can easily use it in my other queries where I am joining tables.John example worked with me..
My table looks something like this.
EmpID | DeductionType | HoursDeduction | DeductionAmount | Year | Month
ABC | F | 4 | $2.00 | 2013 | 10
ABC | H | 2 | $1.50 | 2013 | 10
I my case I have to select two fields on the basis of another field.
SELECT empid,
year1 AS [Year],
month1 AS [Month],
Max(CASE deductiontype
WHEN 'F' THEN hoursdeduction
ELSE 0
END) FullDeductionHours,
Max(CASE deductiontype
WHEN 'F' THEN deductionamount
ELSE 0
END) FullDeductionAmount,
Max(CASE deductiontype
WHEN 'H' THEN hoursdeduction
ELSE 0
END) HalfDeductionHours,
Max(CASE deductiontype
WHEN 'H' THEN deductionamount
ELSE 0
END) HalfHourDeductionAmount
FROM mydeductiontable
GROUP BY empid,
year1,
month1
I only want to know will it be efficient and not slow my stored procedure where I am trying to find deduction hours and using it to calculations for Payroll data. Is there anyway we can make query more smart?