你可以按照不想在输出中显示的列进行排序吗?

huangapple go评论60阅读模式
英文:

How can I Order By a column I don't want displayed in the output?

问题

以下是翻译好的部分:

"我正在尝试生成一个查询,以显示特定分组和顺序的特定数据。我唯一遇到的问题是,为了获得列中数据的正确顺序,我需要按一个未选择且不想显示的列进行排序。

所以当我尝试按该列排序时,会出现以下错误:

“由于未包含在聚合函数或GROUP BY子句中,因此在ORDER BY子句中列**无效。”

是否有一种语法可以让我这样做?以下是我正在使用的查询:"

select Pr.EmployeeNo as EmpNo, EmployeeFName as EmpFName, EmployeeLName as EmpLName,
ProjectName, ProjectStartDate as ProjStartDate, JobName as Job, JobRate, HoursWorked as Hours
from Employee as Em join ProjEmp as Pr on Em.EmployeeNo = Pr.EmployeeNo
					join Project as Pt on Pr.ProjectID = Pt.ProjectID
					join Job as Jb on Em.JobID = Jb.JobID
Group by Pr.EmployeeNo, EmployeeFName, EmployeeLName, ProjectName, ProjectStartDate, JobName, JobRate, HoursWorked
英文:

I'm trying to generate a query to display specific data in specific grouping and order.
The only issue I have is that, to get the proper Order of the data in the columns I need to order it by a column that I have not SELECTED and do not want displayed.

So When I try to ORDER BY that column I get the error:

> "Column ** is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause."

Is there a syntax that will allow me to do this?
Here's the query I'm working with:

select Pr.EmployeeNo as EmpNo, EmployeeFName as EmpFName, EmployeeLName as EmpLName,
ProjectName, ProjectStartDate as ProjStartDate, JobName as Job, JobRate, HoursWorked as Hours
from Employee as Em join ProjEmp as Pr on Em.EmployeeNo = Pr.EmployeeNo
					join Project as Pt on Pr.ProjectID = Pt.ProjectID
					join Job as Jb on Em.JobID = Jb.JobID
Group by Pr.EmployeeNo, EmployeeFName, EmployeeLName, ProjectName, ProjectStartDate, JobName, JobRate, HoursWorked

答案1

得分: 4

停下来,再次阅读完整的错误信息。它会明确告诉你发生了什么。提示:问题不是因为它在SELECT子句中缺失。你可以这样做没有问题:

SELECT ColumnA
FROM [Table1]
ORDER BY ColumnB

问题在于你有一个GROUP BY子句:

在ORDER BY子句中,列是无效的 因为它既不包含在聚合函数中,也不包含在GROUP BY子句中。

这是有道理的。假设你有这张表:

ColumnA ColumnB ColumnC
1 2 3
1 4 9
2 6 7
2 8 5

然后尝试运行这个查询:

SELECT ColumnA, MAX(ColumnB)
FROM [Table]
GROUP BY ColumnA
ORDER BY ColumnC

这个查询试图按ColumnC排序,但每个组中都有多个ColumnC的值!我们在ColumnA上有两个组:12。组1有两个“C”值:39。组2也有两个“C”值:75。根据选择的行,你可能会得到不同的顺序

不过,ColumnA是可以的,因为它是GROUP BY表达式的一部分。这意味着我们知道要使用哪个值。MAX(ColumnB)也可以,因为MAX()是一个聚合函数。它以确定性的方式告诉我们要使用组中的哪个值。但是,ColumnC的引用是模棱两可的,因此不允许。

因此,在问题中的SQL中,你可以在ORDER BY子句中使用这些列中的任何一个:

Pr.EmployeeNo、EmployeeFName、EmployeeLName、ProjectName、ProjectStartDate、JobName、JobRate、HoursWorked

如果你想使用不同的列,你必须要么更改分组(并仔细考虑后果),要么在列组上使用一个聚合函数。

英文:

Stop and read the full error again. It tells you exactly what's going on. Hint: the problem is not that it's missing from the SELECT clause. There's no reason you can't do this:

SELECT ColumnA
FROM [Table1]
ORDER BY ColumnB

The problem is you have a GROUP BY clause:

<pre>Column is invalid in the ORDER BY clause <b><em>because it is not<br/> contained in either an aggregate function or the GROUP BY clause.</em></b></pre>

And this makes sense. Say you have this table:

ColumnA ColumnB ColumnC
1 2 3
1 4 9
2 6 7
2 8 5

And then try to run this query:

SELECT ColumnA, MAX(ColumnB)
FROM [Table]
GROUP BY ColumnA
ORDER BY ColumnC

This query tries to ORDER BY ColumnC, but there's more than one value for ColumnC in each group! We have two groups on ColumnA: 1 and 2. Group 1 has two "C" values: 3 and 9. Group 2 also has two "C" values: 7 and 5. Depending on which rows is selected, you could end up with different orders.

ColumnA, though, is okay, because it's part of the GROUP BY expression. That means we know what value to use. MAX(ColumnB) is also okay, because MAX() is an aggregate function. It tells us which value from the group to use in a deterministic way. But the ColumnC reference is ambiguous(!), and so is not allowed.

So in the SQL from the question, you are free to use any of these columns for the ORDER BY clause:

Pr.EmployeeNo, EmployeeFName, EmployeeLName, ProjectName, ProjectStartDate, JobName, JobRate, HoursWorked

If you want to use a different column, you must either alter the grouping (and think carefully on the consequences) or use an aggregate function on the column group.

答案2

得分: 0

我弄清楚了。答案是将该列添加到GROUP BY中。因为我没有选择该列,所以它不会显示出来。但是通过将该列分组,我可以按其排序,它不会显示出来,因为它实际上并未被选择。

抱歉给您带来麻烦。

英文:

I figured it out. The answer was to add the column to the GROUP BY.
Because I didn't SELECT the column, it won't display. BUT GROUPing BY the column allows me to ORDER BY it, and it won't display because it wasn't actually SELECTED.

Sorry for the trouble.

huangapple
  • 本文由 发表于 2023年2月19日 02:38:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定