Solved

SQL Group by question

Posted on 2013-11-19
2
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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. …
How to use Variables  and Custom code in SSRS report and Assembly reference to use compile shared code in SSRS. Its big question for all who are working with SSRS. It is easy to create assembly and refer in SSRS report, still there are some steps…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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