通过不识别聚合功能来组
我有一个带有这样的记录的表格:
ID | 外国 | 创建 | 状态 |
---|---|---|---|
1 | ZZ01 | 2021-01-20 | 失败 |
2 | ZZ02 | 2021-03-24 | 通过了 |
3 | ZZ01 | 2021-08-09 | 通过了 |
4 | ZZ03 | 2022-01-12 | 失败 |
5 | ZZ01 | 2022-- 04-23 | 通过了 |
我试图编写一个查询,该查询返回上表中每个独特的外国人的最新数据和状态。我一直在编写的查询使用max()查找记录的最新日期,并在状态上使用first_value()也仅获取最新值,然后以我在选择中放置的所有列的组使用组,除了。问题是ii继续遇到此错误:
列'dbo.t.datesubtits'在选择列表中无效,因为它不包含在聚合函数中,或者由子句中的组中包含。
现在,我不明白为什么当我在集合函数中或失败的日期中使用我的组中的所有选择时,此错误会不断弹出。
SELECT [T].[Id],
[T].[ForeignId],
MAX([T].[DateSubmitted]) AS [Date Submitted],
FIRST_VALUE([S].[Status]) OVER (ORDER BY [T].[DateSubmitted] DESC) AS [Status]
FROM Table as [T]
GROUP BY [T].[Id], [T].[ForeignId], [T].[Status]
ORDER BY [T].[ForeignId];
从上面的代码中可以看到,我正在使用jatatesubits的select使用max()函数,而我的所有剩菜选择都在组中,我缺少什么?为什么我只选择最大值()值时,我需要将我的日期包含在组中?
我想要的是仅返回每个不同外国人的最新日期和状态,因为每个外国人都可以具有多个状态和日期,我只需要最新的值by and max()我可以获取我重复外国实例的最新列信息。例如,我会收到3次而不是一次。这就是为什么我需要小组上班的原因。无法使用组时不良输出的一个示例:
ID | foreferid | 日期创建 | 状态 |
---|---|---|---|
1 | ZZ01 | 2021-04-23 | 通过了 |
2 | ZZ01 | 2021-04-23 | 通过了 |
3 | ZZ01 | 2021-04-23 | 通过了 |
4 | ZZ02 | 2022-03-24 | 通过了 |
5 | ZZ03 | 2022-01-12 | 失败的 |
预期结果:
ID | 外国 | 创建的 | 状态 |
---|---|---|---|
1 | ZZ01 | 2021-04-23 | 通过了 |
2 | ZZ02 | 2022-03-24 | 通过了 |
3 | ZZ03 | 2022-01-12 | 失败 |
I have a Table with records like this:
Id | ForeignId | Date Created | Status |
---|---|---|---|
1 | ZZ01 | 2021-01-20 | failed |
2 | ZZ02 | 2021-03-24 | passed |
3 | ZZ01 | 2021-08-09 | passed |
4 | ZZ03 | 2022-01-12 | failed |
5 | ZZ01 | 2022-04-23 | passed |
I am trying to write a query that returns the latest DateCreated and Status of every Distinct ForeignId in the table above. The query I have been writing uses MAX() to find the latest date of a record, and uses FIRST_VALUE() on Status to get only the latest value as well, and later using GROUP BY all the columns I put in the SELECT except for . The problem is that I I keep getting this error:
Column 'dbo.T.DateSubmitted' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Now I don't understand why this error keeps popping up when I am using all my Selects in the Group By or in the case of the failing DateSubmitted in an Aggregate Function.
SELECT [T].[Id],
[T].[ForeignId],
MAX([T].[DateSubmitted]) AS [Date Submitted],
FIRST_VALUE([S].[Status]) OVER (ORDER BY [T].[DateSubmitted] DESC) AS [Status]
FROM Table as [T]
GROUP BY [T].[Id], [T].[ForeignId], [T].[Status]
ORDER BY [T].[ForeignId];
As you can see from my code above I am using the MAX() function for my DateSubmitted select, and all my leftover selects in the GROUP BY, what am I missing? Why do I need to include my DateSubmitted in the GROUP BY when I am only selecting the MAX() value?
What I want is to return only the latest date and status for each distinct ForeignId, since every ForeignId can have multiple Status and Dates I want only the latest values, I almost get it with the query above, but if I am unable to use the GROUP BY and MAX() I can get the latest column information I have repeated ForeignId instances. For example I would receive ForeignId ZZ01 3 times instead of once. Which is why I need the GROUP BY to work. An example of the bad output when unable to use GROUP BY:
Id | ForeignId | Date Created | Status |
---|---|---|---|
1 | ZZ01 | 2021-04-23 | passed |
2 | ZZ01 | 2021-04-23 | passed |
3 | ZZ01 | 2021-04-23 | passed |
4 | ZZ02 | 2022-03-24 | passed |
5 | ZZ03 | 2022-01-12 | failed |
Expected Result:
Id | ForeignId | Date Created | Status |
---|---|---|---|
1 | ZZ01 | 2021-04-23 | passed |
2 | ZZ02 | 2022-03-24 | passed |
3 | ZZ03 | 2022-01-12 | failed |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,
first_value
不作为汇总函数可用,而仅作为窗口函数。因此,编译器将其理解为在汇总之后的结果集上运行的。因此,所指的列必须在的组中,但不能放在非聚集和非组列上。
您可以通过汇总函数使用它,
这可能不会给出您想要的结果,如果没有示例数据,很难说。
或者,您需要将其向下推入派生表(子查询)。您可以在整个连接的集合中执行此操作
,也可以通过一个表在一个表格上进行操作,并通过添加条款的
分区来加入它。
Unfortunately,
FIRST_VALUE
is not available as an aggregate function, only as a window function. Therefore, the compiler understands it as operating over the resultset after aggregating. So the columns referred to must be in theGROUP BY
, but cannot be on non-aggregated and non-grouped columns.You can use it over an aggregate function
This may not give the results you want, it's hard to say without sample data.
Or you need to push it down into a derived table (subquery). You can do this over the whole joined set
Or you can do it just over the one table and join it after, by adding a
PARTITION BY
clause.