MAX() 和 MAX() OVER PARTITION BY 在 Teradata 查询中产生错误 3504
我正在尝试生成一个结果表,其中包含每个课程代码的最后完成的课程日期,以及每个员工最后完成的课程代码。下面是我的查询:
SELECT employee_number,
MAX(course_completion_date)
OVER (PARTITION BY course_code) AS max_course_date,
MAX(course_completion_date) AS max_date
FROM employee_course_completion
WHERE course_code IN ('M910303', 'M91301R', 'M91301P')
GROUP BY employee_number
此查询产生以下错误:
3504 : Selected non-aggregate values must be part of the associated group
如果我删除 MAX() OVER (PARTITION BY...) 行,查询执行得很好,所以我已将问题隔离到该行,但在搜索这些之后论坛和互联网我看不出我做错了什么。有人可以帮忙吗?
I am trying to produce a results table with the last completed course date for each course code, as well as the last completed course code overall for each employee. Below is my query:
SELECT employee_number,
MAX(course_completion_date)
OVER (PARTITION BY course_code) AS max_course_date,
MAX(course_completion_date) AS max_date
FROM employee_course_completion
WHERE course_code IN ('M910303', 'M91301R', 'M91301P')
GROUP BY employee_number
This query produces the following error:
3504 : Selected non-aggregate values must be part of the associated group
If I remove the MAX() OVER (PARTITION BY...) line, the query executes just fine, so I've isolated the problem to that line, but after searching these forums and the internet I can't see what I'm doing wrong. Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
逻辑上OLAP函数是在GROUP BY/HAVING之后计算的,因此只能访问GROUP BY中的列或具有聚合函数的列。以下看起来很奇怪,但却是标准 SQL:
并且由于 Teradata 允许重复使用别名,这也有效:
Logically OLAP functions are calculated after GROUP BY/HAVING, so you can only access columns in GROUP BY or columns with an aggregate function. Following looks strange, but is Standard SQL:
And as Teradata allows re-using an alias this also works:
正如 Ponies 在评论中所说,不能将 OLAP 函数与聚合函数混合在一起。
也许更容易获得每个员工的最后完成日期,并将其加入到包含三个目标课程中每门课程的最后完成日期的数据集。
这是一个未经测试的想法,希望能让您走上正确的道路:
As Ponies says in a comment, you cannot mix OLAP functions with aggregate functions.
Perhaps it's easier to get the last completion date for each employee, and join that to a dataset containing the last completion date for each of the three targeted courses.
This is an untested idea that should hopefully put you down the right path:
我知道这是一个非常古老的问题,但其他人也问过我类似的问题。
我没有 TeraData,但是您不能执行以下操作吗?
GROUP BY
现在可确保每位员工每门课程一行。这意味着您只需要直接MAX()
即可获取max_course_date
。在您的 GROUP BY 只为每个员工提供一行之前,
MAX() OVER()
试图为该行提供多个结果(每个员工一个)当然)。相反,您现在需要
OVER()
子句来获取整个员工的MAX()
。现在这是合法的,因为每一行仅得到一个答案(因为它来自超集,而不是子集)。此外,出于同样的原因,OVER()
子句现在引用由GROUP BY
子句定义的有效标量值;员工编号
。也许简单地说,带有
OVER()
子句的aggregate
必须是GROUP BY
的超集code>,不是子集。在表示所需行的级别使用
GROUP BY
创建查询,然后如果要在更高级别聚合,请指定OVER()
子句。I know this is a very old question, but I've been asked by someone else something similar.
I don't have TeraData, but can't you do the following?
The
GROUP BY
now ensures one row per course per employee. This means that you just need a straightMAX()
to get themax_course_date
.Before your
GROUP BY
was just giving one row per employee, and theMAX() OVER()
was trying to give multiple results for that one row (one per course).Instead, you now need the
OVER()
clause to get theMAX()
for the employee as a whole. This is now legitimate because each individual row gets just one answer (as it is derived from a super-set, not a sub-set). Also, for the same reason, theOVER()
clause now refers to a valid scalar value, as defined by theGROUP BY
clause;employee_number
.Perhaps a short way of saying this would be that an
aggregate
with anOVER()
clause must be a super-set of theGROUP BY
, not a sub-set.Create your query with a
GROUP BY
at the level that represents the rows you want, then specifyOVER()
clauses if you want to aggregate at a higher level.我认为这会起作用,即使这已经是很久以前的事了。
如果您想在日期相同的情况下获取最后一个 Id,那么您可以使用它,假设您的主键是 Id。
I think this will work even though this was forever ago.
If you want to get the last Id if the date is the same then you can use this assuming your primary key is Id.