Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Totalling Columns

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 | Accrual5

Now i need a Total_Accrual Column

This is my query code so far:

SELECT [EmpID],
[First_Name],
[Surname],
[Paygroup],
[ContHrs],
[StartDate],
CASE
WHEN [ContHrs1] IS NUll THEN [ContHrs]*[Hols_Calc]/365* DATEDIFF(Day,[StartDate], GETDATE())
ELSE ([ContHrs1]*[Hols_Calc1])/365* DATEDIFF(day,[StartDate],[AmendDate1])
END
As 'Accrual0',
CASE
WHEN [ContHrs2] IS NUll THEN [ContHrs1]*[Hols_Calc1]/365* DATEDIFF(Day,[AmendDate1], GETDATE())
ELSE ([ContHrs2]*[Hols_Calc2])/365* DATEDIFF(day,[AmendDate1],[AmendDate2])
END
As 'Accrual1',
CASE
WHEN [ContHrs3] IS NUll THEN [ContHrs2]*[Hols_Calc2]/365* DATEDIFF(Day,[AmendDate2], GETDATE())
ELSE ([ContHrs3]*[Hols_Calc3])/365* DATEDIFF(day,[AmendDate2],[AmendDate3])
END
As 'Accrual2',
CASE
WHEN [ContHrs4] IS NUll THEN [ContHrs3]*[Hols_Calc3]/365* DATEDIFF(Day,[AmendDate3], GETDATE())
ELSE ([ContHrs4]*[Hols_Calc4])/365* DATEDIFF(day,[AmendDate3],[AmendDate4])
END
As 'Accrual3',
CASE
WHEN [ContHrs5] IS NUll THEN [ContHrs4]*[Hols_Calc4]/365* DATEDIFF(Day,[AmendDate4], GETDATE())
ELSE ([ContHrs5]*[Hols_Calc5])/365* DATEDIFF(day,[AmendDate4],[AmendDate5])
END
As 'Accrual4',
CASE
WHEN [ContHrs5] IS NOT NUll THEN [ContHrs5]*[Hols_Calc5]/365* DATEDIFF(Day,[AmendDate5], GETDATE())
END
As '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 | Accrual5

Now i need a Total_Accrual Column

This is my query code so far:

SELECT [EmpID],
[First_Name],
[Surname],
[Paygroup],
[ContHrs],
[StartDate],
SUM(CASE
WHEN [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(CASE
WHEN [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(CASE
WHEN [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(CASE
WHEN [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(CASE
WHEN [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(CASE
WHEN [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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 tbl
group by EmpID

I'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.
Go to Top of Page
   

- Advertisement -