titans4771
MIS
In the code below, I am trying to sum a couple of my columns that are using case statements. The column at the bottom, PTEFee is the column where I am trying to sum everything up. Here is the code:
Select
'TotalMilieage' = SUM(case when y.paycodeid = 261 then isnull(y.units,0) else 0 end),
'MileageRate' = SUM(Case when y.paycodeid = 261 then isnull(y.billrate,0) else 0 end),
'ExpFee' = SUM(case when y.paycodeid = 259 and y.computeonunits = 1 then (y.billunits*y.billrate)
when y.paycodeid = 259 then ISNULL(y.billrate,0) end),
'TotalExpenseAmount' = SUM(case when y.paycodeid = 259 and y.computeonunits = 1 then (y.units*y.payrate)
when y.paycodeid = 259 then ISNULL(y.payrate,0) end),
'BonusFee' = SUM(case when y.paycodeid in (33,286,337,1959) and y.computeonunits = 1 then (y.billunits*y.billrate)
when y.paycodeid in (33,286,337,1959) then ISNULL(y.billrate,0) end),
'BonusAmount' = SUM(case when y.paycodeid in (33,286,337,1959) and computeonunits = 1 then (y.units*y.payrate)
when y.paycodeid in (33,286,337,1959) then ISNULL(y.payrate,0) end),
'IncentiveFee' = SUM(case when y.paycodeid in(260,264) and y.computeonunits = 1 then (y.billunits*y.billrate)
when y.paycodeid in(260,264) then ISNULL(y.billrate,0) end),
'TotalIncentiveAmount' = SUM(case when y.paycodeid in(260,264) and y.computeonunits = 1 then (y.units*y.payrate)
when y.paycodeid in(260,264) then y.payrate end),
'Regular Hours' = SUM(case when y.paycodeid = 1 then ISNULL(y.units,0) else 0 end),
'Overtime Hours' = SUM(case when y.paycodeid = 2 then ISNULL(y.units,0) else 0 end),
'TotalHours' = SUM(case when y.paycodeid = 1 then isnull(y.units,0) else 0 end)+SUM(case when y.paycodeid = 2 then isnull(y.units,0) else 0 end),
'PTEFee' = (SUM(case when y.paycodeid = 259 and y.computeonunits = 1 then (y.billunits*y.billrate)
when y.paycodeid = 259 then ISNULL(y.billrate,0) end)+
SUM(case when y.paycodeid in (33,286,337,1959) and y.computeonunits = 1 then (y.billunits*y.billrate)
when y.paycodeid in (33,286,337,1959) then ISNULL(y.billrate,0) end)+
SUM(case when y.paycodeid in(260,264) and y.computeonunits = 1 then (y.billunits*y.billrate)
when y.paycodeid in(260,264) then ISNULL(y.billrate,0) end))
from
emp e
inner join workorder w on w.employeeid = e.employeeid
inner join checkheader p on p.employeeid = e.employeeid
inner join
(
Select
checkhistid, r.units, r.woid, r.paycodeid, r.payrate, computeonunits, billrate, billunits
from checktc r
inner join tcdetail t on t.tcdetailid = r.tcdetailid
inner join tcheadera h on h.tcid = t.tcid
inner join cust c on c.custid = r.custid
inner join paycodes s on s.paycodeid = r.paycodeid
where startdate between '2008-2-1' and '2008-2-4' and
r.paycodeid in (1,2,259,260,264,33,286,337,1959) and
r.custid = 1 and employeeid = 39489
) y on y.checkhistid = p.checkhistid and y.woid = w.woid
Please let me know what I am doing wrong as to why the PTEFee column is not adding up correctly. It returns NULL in that field
Thanks!!
Select
'TotalMilieage' = SUM(case when y.paycodeid = 261 then isnull(y.units,0) else 0 end),
'MileageRate' = SUM(Case when y.paycodeid = 261 then isnull(y.billrate,0) else 0 end),
'ExpFee' = SUM(case when y.paycodeid = 259 and y.computeonunits = 1 then (y.billunits*y.billrate)
when y.paycodeid = 259 then ISNULL(y.billrate,0) end),
'TotalExpenseAmount' = SUM(case when y.paycodeid = 259 and y.computeonunits = 1 then (y.units*y.payrate)
when y.paycodeid = 259 then ISNULL(y.payrate,0) end),
'BonusFee' = SUM(case when y.paycodeid in (33,286,337,1959) and y.computeonunits = 1 then (y.billunits*y.billrate)
when y.paycodeid in (33,286,337,1959) then ISNULL(y.billrate,0) end),
'BonusAmount' = SUM(case when y.paycodeid in (33,286,337,1959) and computeonunits = 1 then (y.units*y.payrate)
when y.paycodeid in (33,286,337,1959) then ISNULL(y.payrate,0) end),
'IncentiveFee' = SUM(case when y.paycodeid in(260,264) and y.computeonunits = 1 then (y.billunits*y.billrate)
when y.paycodeid in(260,264) then ISNULL(y.billrate,0) end),
'TotalIncentiveAmount' = SUM(case when y.paycodeid in(260,264) and y.computeonunits = 1 then (y.units*y.payrate)
when y.paycodeid in(260,264) then y.payrate end),
'Regular Hours' = SUM(case when y.paycodeid = 1 then ISNULL(y.units,0) else 0 end),
'Overtime Hours' = SUM(case when y.paycodeid = 2 then ISNULL(y.units,0) else 0 end),
'TotalHours' = SUM(case when y.paycodeid = 1 then isnull(y.units,0) else 0 end)+SUM(case when y.paycodeid = 2 then isnull(y.units,0) else 0 end),
'PTEFee' = (SUM(case when y.paycodeid = 259 and y.computeonunits = 1 then (y.billunits*y.billrate)
when y.paycodeid = 259 then ISNULL(y.billrate,0) end)+
SUM(case when y.paycodeid in (33,286,337,1959) and y.computeonunits = 1 then (y.billunits*y.billrate)
when y.paycodeid in (33,286,337,1959) then ISNULL(y.billrate,0) end)+
SUM(case when y.paycodeid in(260,264) and y.computeonunits = 1 then (y.billunits*y.billrate)
when y.paycodeid in(260,264) then ISNULL(y.billrate,0) end))
from
emp e
inner join workorder w on w.employeeid = e.employeeid
inner join checkheader p on p.employeeid = e.employeeid
inner join
(
Select
checkhistid, r.units, r.woid, r.paycodeid, r.payrate, computeonunits, billrate, billunits
from checktc r
inner join tcdetail t on t.tcdetailid = r.tcdetailid
inner join tcheadera h on h.tcid = t.tcid
inner join cust c on c.custid = r.custid
inner join paycodes s on s.paycodeid = r.paycodeid
where startdate between '2008-2-1' and '2008-2-4' and
r.paycodeid in (1,2,259,260,264,33,286,337,1959) and
r.custid = 1 and employeeid = 39489
) y on y.checkhistid = p.checkhistid and y.woid = w.woid
Please let me know what I am doing wrong as to why the PTEFee column is not adding up correctly. It returns NULL in that field
Thanks!!