I am trying to somehow group a report based on a drop-down list of parameters that is pre-defined. I want to be able to subtotal the Total Hours or Total Pay of my report based on Department or JobCode. I have created the parameters and have no problem with that, I just am not sure if it's possible to use those parameters to call out a grouping command. Below is the spirit of what I am wanting, but the GROUP BY clause doesn't work for me even without a parameter.
SELECT EmployeeID, LastName, FirstName, Department, JobCode, PayRate, SUM(Hours) as "Total Hours", SUM(Pay) as "Total Pay"
FROM Employees
GROUP BY @GroupBy
I am truly a novice when it comes to SQL, so any help is very much appreciated.
Thank You.
Group byis simple really.You have to list in
group byevery field that is included in the select statement and not fed to an aggregate function.Which is why you can't have a variable
group bywith a fixed list of columns inselect. (Well, you can in mysql, but it effectively applies virtualany()aggregate to them.)