We help IT Professionals succeed at work.

Sum field based on another field.

BobRosas asked
Last Modified: 2012-02-15
I want to total HoursWorked if the "EarningCode" is "REG", "HOL" or "PTO" but I want the HoursWorked separate for "OVT".  With the following code I was able to get the Earning Code to change but now if won't sum the HoursWorked.  I'm using Report Services with SQL code.  The results I have look like this...
1/10/2012 12:00:00 AM   AS0142   JENS1787   8.000000000   0.000000000   REG   0.000000000
1/10/2012 12:00:00 AM   AS0142   JENS1787  77.630000000  0.000000000        REG   0.000000000
1/10/2012 12:00:00 AM   AS0142   JENS1787   0.1200000000  0.000000000        OVT   0.000000000

What I want is this...
1/10/2012 12:00:00 AM   AS0142   JENS1787  85.630000000  0.000000000        REG   0.000000000
1/10/2012 12:00:00 AM   AS0142   JENS1787   0.1200000000  0.000000000        OVT   0.000000000

SELECT        tblPaCheckHist.CheckDate, tblPaCheckHist.DepartmentId, tblPaCheckHist.EmployeeId, SUM(tblPaCheckHistEarn.HoursWorked) AS HoursWorked,
                         CASE WHEN LaborClass = 'CL' THEN tblPaCheckHistEarn.HourlyRate ELSE 0 END AS RatePaid,
                         CASE WHEN EarningCode = 'HOL' THEN 'REG' WHEN EarningCode = 'PTO' THEN 'REG' ELSE EarningCode END AS EarningCode,
                         CASE WHEN LaborClass = 'CL' THEN SUM(tblPaCheckHistEarn.EarningsAmount) ELSE 0 END AS Earnings
FROM            tblPaCheckHistEarn INNER JOIN
                         tblPaCheckHist ON tblPaCheckHistEarn.PostRun = tblPaCheckHist.PostRun AND tblPaCheckHistEarn.SequenceNumber = tblPaCheckHist.SequenceNumber
WHERE        (DATEADD(d, - 15, tblPaCheckHist.CheckDate) BETWEEN @From AND @To) AND (tblPaCheckHist.EmployeeId = 'JENS1787')
GROUP BY tblPaCheckHist.CheckDate, tblPaCheckHist.DepartmentId, tblPaCheckHist.EmployeeId, tblPaCheckHist.EmployeeName, tblPaCheckHistEarn.HourlyRate,
                         tblPaCheckHistEarn.LaborClass, tblPaCheckHistEarn.EarningCode
ORDER BY tblPaCheckHist.DepartmentId, tblPaCheckHist.EmployeeName
Watch Question

BI Consultant
Most Valuable Expert 2011
This one is on us!
(Get your first solution completely free - no credit card required)


That works!  Thank you so much for you help!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.