Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Group by question

Posted on 2013-11-19
2
Medium Priority
?
355 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 25

Accepted Solution

by:
chaau earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code started out as a fix for a customer that had incoming data that was hunderds of numbers and words long that was to fit in one column. The problem was that the customer did not want to split words or numbers when wrapping in the column. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

885 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