在 SQL 中使用参数进行 GROUP BY

发布于 2024-12-27 02:08:21 字数 377 浏览 2 评论 0原文

我试图根据预定义的参数下拉列表以某种方式对报告进行分组。我希望能够根据部门或职位代码对我的报告的总工时或总工资进行小计。我已经创建了参数并且没有任何问题,我只是不确定是否可以使用这些参数来调用分组命令。以下是我想要的精神,但即使没有参数,GROUP BY 子句对我也不起作用。

SELECT EmployeeID, LastName, FirstName, Department, JobCode, PayRate, SUM(Hours) as "Total Hours", SUM(Pay) as "Total Pay"
FROM Employees
GROUP BY @GroupBy

在 SQL 方面,我确实是一个新手,所以非常感谢任何帮助。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情深已缘浅 2025-01-03 02:08:21

这个要求对我来说并不是 100% 清楚,但我想你会经历这样的事情:

select
  case when @groupBy = 'dept' then
       department else
       jobCode    end  dept_jobCode,
  sum(hours)
from employees
group by 
  case when @groupBy = 'dept' then
       department else
       jobCode    end;

要尝试这个设置:

create table employees (
  lastName   varchar(20),
  department varchar(20),
  jobCode    varchar(20),
  hours      number
);

insert into employees values ('Miller', 'Dept 1', 'A', 10);
insert into employees values ('Doe'   , 'Dept 1', 'A',  7);
insert into employees values ('Baker' , 'Dept 1', 'B',  4);

insert into employees values ('Sand'  , 'Dept 2', 'B',  6);
insert into employees values ('Stark' , 'Dept 2', 'B',  9);
insert into employees values ('Gild'  , 'Dept 2', 'A',  9);

显然,你想将 @groupBy 设置为 'dept'< /code> 或任何其他值。

The requirement is not 100% clear to me, but I imagine your after something like this:

select
  case when @groupBy = 'dept' then
       department else
       jobCode    end  dept_jobCode,
  sum(hours)
from employees
group by 
  case when @groupBy = 'dept' then
       department else
       jobCode    end;

To be tried with this setup:

create table employees (
  lastName   varchar(20),
  department varchar(20),
  jobCode    varchar(20),
  hours      number
);

insert into employees values ('Miller', 'Dept 1', 'A', 10);
insert into employees values ('Doe'   , 'Dept 1', 'A',  7);
insert into employees values ('Baker' , 'Dept 1', 'B',  4);

insert into employees values ('Sand'  , 'Dept 2', 'B',  6);
insert into employees values ('Stark' , 'Dept 2', 'B',  9);
insert into employees values ('Gild'  , 'Dept 2', 'A',  9);

Obviously, you want to set @groupBy to either 'dept' or any other value.

雅心素梦 2025-01-03 02:08:21

我认为您从根本上误解了 GROUP BY 的工作原理。

GROUPING 是将多行聚合在一起的一种方法。

如果您返回的任何字段不在 GROUP BY 中,您需要决定如何处理它们。您不能对它们执行任何操作,因为每个组可能有多个值。它们必须被排除或聚合。

要聚合它们,您需要决定使用什么函数(MAXMINSUMAVG 等)。

如果您想要显示许多行,例如每个员工一行,但想要包含有关该员工所在部门的总计的一些信息,则需要使用子查询:

SELECT employeeid, <other unaggregated fields>
FROM MyTable t
INNER JOIN (SELECT DepartmentID, SUM(Totalhours) as TotHours...etc
            FROM SomeOtherTable
            GROUP BY DepartmentID) as Sub
  ON Sub.DepartmentID = t.departmentID

可能有一种方法可以动态地执行此操作,但这是一个非常糟糕的主意。

I think you are fundamentally misunderstanding how GROUP BY works.

GROUPING is a way to aggregate many rows together.

If you return any fields not in the GROUP BY, you need to decide what to do with them. You can't NOT do anything with them because you could have multiple values per group. They must be either excluded or aggregated.

To aggregate them, you need to decide what function to use (MAX, MIN, SUM, AVG, etc).

If you want to show many rows, say one per employee, but want to include some information about totals for that employee's department, you need to use a subquery:

SELECT employeeid, <other unaggregated fields>
FROM MyTable t
INNER JOIN (SELECT DepartmentID, SUM(Totalhours) as TotHours...etc
            FROM SomeOtherTable
            GROUP BY DepartmentID) as Sub
  ON Sub.DepartmentID = t.departmentID

There may be a way to do this dynamically but that is a pretty poor idea.

や莫失莫忘 2025-01-03 02:08:21

Group by 确实很简单。

您必须在 group by 中列出 select 语句中包含的且未提供给聚合函数的每个字段。

这就是为什么您不能在 select 中使用带有固定列列表的变量 group by。 (嗯,你可以在 mysql 中,但它有效地将虚拟 any() 聚合应用于它们。)

Group by is simple really.

You have to list in group by every 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 by with a fixed list of columns in select. (Well, you can in mysql, but it effectively applies virtual any() aggregate to them.)

不再见 2025-01-03 02:08:21

您选择的任何未由聚合函数(SUM、MIN 等)之一使用的列都需要在 GROUP BY 子句中列出。

例如,

SELECT EmployeeID, LastName, FirstName, SUM(Hours) as "Total Hours"
FROM Employees
GROUP BY EmployeeID, LastName, FirstName

这里有很好的例子: http://www.w3schools.com/sql/sql_groupby.asp< /a>

Any column you are selecting that are not used by one of the aggregate function (SUM, MIN, etc) needs to be listed in the GROUP BY clause.

For example,

SELECT EmployeeID, LastName, FirstName, SUM(Hours) as "Total Hours"
FROM Employees
GROUP BY EmployeeID, LastName, FirstName

Good examples here: http://www.w3schools.com/sql/sql_groupby.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文