Solved

SQL Group by question

Posted on 2013-11-19
2
327 Views
Last Modified: 2013-11-19
I have a query for SSRS to detail employee bonuses for the past 3 years.

I achieved the appropriate bonus amounts using case statements, but I want the result in one row for each employee.  Currently, with the attached query, it reports in 3 rows.  If I take the Payroll Date out of the Group by clause, it errors.
groupby.txt
0
Comment
Question by:urthrilled
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39660929
to remove the datepart component from grouping you need to include it inside the aggregate function, like this:
SELECT     PREH.FirstName, PREH.Employee, PREH.udDivision, PREH.LastName, PREH.GLCo, PREH.HrlyRate, 
              sum(case  when (datepart(yyyy,PRDT.PREndDate)=(2012-2)) then PRDT.Amount else 0 end) as TwoPrvYr,
		sum(case  when (datepart(yyyy,PRDT.PREndDate)=(2012-1)) then PRDT.Amount else 0 end) as PrvYr,
		 sum(case  when (datepart(yyyy,PRDT.PREndDate)=(2012)) then PRDT.Amount else 0 end) as Year

FROM         HRRM INNER JOIN
                      PREH ON HRRM.HRCo = PREH.PRCo AND HRRM.HRRef = PREH.Employee INNER JOIN
                      HRPC ON PREH.PRCo = HRPC.HRCo AND HRRM.PositionCode = HRPC.PositionCode INNER JOIN
                      HQCO ON PREH.GLCo = HQCO.HQCo LEFT OUTER JOIN
                      PRDT ON PREH.PRCo = PRDT.PRCo AND PREH.PRGroup = PRDT.PRGroup AND PREH.Employee = PRDT.Employee

where  PREH.ActiveYN = 'Y' AND (NOT (PREH.GLCo IN (90, 201))) AND (PRDT.EDLCode = 10) and datepart(yyyy,PRDT.PREndDate) between (2012)-2 and (2012)
GROUP BY PREH.GLCo, PREH.udDivision, PREH.LastName, PREH.FirstName, PREH.HrlyRate, PREH.Employee
ORDER BY PREH.GLCo, PREH.udDivision, PREH.LastName, PREH.Employee

Open in new window


On a side note, this statement:
 datepart(yyyy,PRDT.PREndDate) between (2012)-2 and (2012)

Open in new window

is not great in terms of performace, as SQL Server will not be able to construct index. It will be better to compare dates with dates, like this:
PRDT.PREndDate >= '20100101' and PRDT.PREndDate < '20130101'

Open in new window

(this statement will grab all dates for years 2010, 2011, 2012)
0
 
LVL 4

Author Closing Comment

by:urthrilled
ID: 39661111
Thank you!  I am used to manipulating with Crystal and am just gaining the in depth SQL experience to eloquently create SSRS reports.  I do like them so much better, but old dogs can't always readily see new tricks!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this short article I will be talking about two functions in the SQL Server Reporting Services (SSRS) function stack.  Those functions are IIF() and Switch().  And I'll be showing you how easy it is to add an Else part to the Switch function. T…
Hi All, I am here to write a simple article to move SSRS (SQL Server Reporting Services) reports from one server to another. When I have faced the same issue to move reports those were developed by developer on development server and now need to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now