Author |
Topic |
Badeye
Starting Member
1 Post |
Posted - 2011-07-21 : 11:57:29
|
Hi All,I'm very new to SQL but seem to be slowly working my way there. I have a table with information of employees and there working contracted hours as i am building a database to hold and calculate employees holiday entitlement. At the moment i'm working on the accruals which i have done pretty successfully but now i have my query to calculate the accrual i now need a column in the results to add these accruals up per person. Can anyone help?My output looks like this:EmpID | EmpName | Accrual0 | Accrual1 | Accrual2 | Accrual3 | Accrual4 | Accrual5Now i need a Total_Accrual ColumnThis is my query code so far:SELECT [EmpID],[First_Name],[Surname],[Paygroup],[ContHrs],[StartDate],CASEWHEN [ContHrs1] IS NUll THEN [ContHrs]*[Hols_Calc]/365* DATEDIFF(Day,[StartDate], GETDATE())ELSE ([ContHrs1]*[Hols_Calc1])/365* DATEDIFF(day,[StartDate],[AmendDate1])ENDAs 'Accrual0',CASEWHEN [ContHrs2] IS NUll THEN [ContHrs1]*[Hols_Calc1]/365* DATEDIFF(Day,[AmendDate1], GETDATE())ELSE ([ContHrs2]*[Hols_Calc2])/365* DATEDIFF(day,[AmendDate1],[AmendDate2])ENDAs 'Accrual1',CASEWHEN [ContHrs3] IS NUll THEN [ContHrs2]*[Hols_Calc2]/365* DATEDIFF(Day,[AmendDate2], GETDATE())ELSE ([ContHrs3]*[Hols_Calc3])/365* DATEDIFF(day,[AmendDate2],[AmendDate3])ENDAs 'Accrual2',CASEWHEN [ContHrs4] IS NUll THEN [ContHrs3]*[Hols_Calc3]/365* DATEDIFF(Day,[AmendDate3], GETDATE())ELSE ([ContHrs4]*[Hols_Calc4])/365* DATEDIFF(day,[AmendDate3],[AmendDate4])ENDAs 'Accrual3',CASEWHEN [ContHrs5] IS NUll THEN [ContHrs4]*[Hols_Calc4]/365* DATEDIFF(Day,[AmendDate4], GETDATE())ELSE ([ContHrs5]*[Hols_Calc5])/365* DATEDIFF(day,[AmendDate4],[AmendDate5])ENDAs 'Accrual4',CASEWHEN [ContHrs5] IS NOT NUll THEN [ContHrs5]*[Hols_Calc5]/365* DATEDIFF(Day,[AmendDate5], GETDATE())ENDAs 'Accrual5'FROM [LabourReportingTest].[dbo].[Emp_Holidays]Any Help, would be great.Thanks,Si |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-07-21 : 12:07:02
|
quote: Originally posted by Badeye Hi All,I'm very new to SQL but seem to be slowly working my way there. I have a table with information of employees and there working contracted hours as i am building a database to hold and calculate employees holiday entitlement. At the moment i'm working on the accruals which i have done pretty successfully but now i have my query to calculate the accrual i now need a column in the results to add these accruals up per person. Can anyone help?My output looks like this:EmpID | EmpName | Accrual0 | Accrual1 | Accrual2 | Accrual3 | Accrual4 | Accrual5Now i need a Total_Accrual ColumnThis is my query code so far:SELECT [EmpID],[First_Name],[Surname],[Paygroup],[ContHrs],[StartDate],SUM(CASEWHEN [ContHrs1] IS NUll THEN [ContHrs]*[Hols_Calc]/365* DATEDIFF(Day,[StartDate], GETDATE())ELSE ([ContHrs1]*[Hols_Calc1])/365* DATEDIFF(day,[StartDate],[AmendDate1])END)As 'Accrual0',SUM(CASEWHEN [ContHrs2] IS NUll THEN [ContHrs1]*[Hols_Calc1]/365* DATEDIFF(Day,[AmendDate1], GETDATE())ELSE ([ContHrs2]*[Hols_Calc2])/365* DATEDIFF(day,[AmendDate1],[AmendDate2])END)As 'Accrual1',SUM(CASEWHEN [ContHrs3] IS NUll THEN [ContHrs2]*[Hols_Calc2]/365* DATEDIFF(Day,[AmendDate2], GETDATE())ELSE ([ContHrs3]*[Hols_Calc3])/365* DATEDIFF(day,[AmendDate2],[AmendDate3])END)As 'Accrual2',SUM(CASEWHEN [ContHrs4] IS NUll THEN [ContHrs3]*[Hols_Calc3]/365* DATEDIFF(Day,[AmendDate3], GETDATE())ELSE ([ContHrs4]*[Hols_Calc4])/365* DATEDIFF(day,[AmendDate3],[AmendDate4])END)As 'Accrual3',SUM(CASEWHEN [ContHrs5] IS NUll THEN [ContHrs4]*[Hols_Calc4]/365* DATEDIFF(Day,[AmendDate4], GETDATE())ELSE ([ContHrs5]*[Hols_Calc5])/365* DATEDIFF(day,[AmendDate4],[AmendDate5])END)As 'Accrual4',SUM(CASEWHEN [ContHrs5] IS NOT NUll THEN [ContHrs5]*[Hols_Calc5]/365* DATEDIFF(Day,[AmendDate5], GETDATE())END)As 'Accrual5'FROM [LabourReportingTest].[dbo].[Emp_Holidays]GROUP BY [EmpID],[First_Name],[Surname],[Paygroup],[ContHrs],[StartDate]Any Help, would be great.Thanks,Si
sounds like small editing above------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-07-21 : 12:09:04
|
SELECT [EmpID],max([First_Name]),max([Surname]),max([Paygroup]),sum([ContHrs]),max([StartDate]),sum(coalesce(ContHrs*[Hols_Calc],ContHrs1)*[Hols_Calc1])/365* DATEDIFF(Day,[StartDate], GETDATE()))As 'Accrual0',...from tblgroup by EmpIDI'm guessing that ContHrs and ContHrs1 is per row rather than static for the empid.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
|
|
|