Solved

SQL Group by question

Posted on 2013-11-19
2
331 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: This article is aimed at report developers who are used to developing reports using relational databases and have gotten a first-time assignment to develop reports on OLAP cubes. It demonstrates how to build a report using SQL Ser…
Introduction As you'll probably know, a data region in a SQL Server Reporting Services report can be linked to only one dataset.  This makes it troublesome when you need to display data from more than one dataset in the same data region.  SQL Serve…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

18 Experts available now in Live!

Get 1:1 Help Now