通过不识别聚合功能来组

发布于 2025-01-26 03:40:44 字数 2178 浏览 3 评论 0原文

我有一个带有这样的记录的表格:

ID外国创建状态
1ZZ012021-01-20失败
2ZZ022021-03-24通过了
3ZZ012021-08-09通过了
4ZZ032022-01-12失败
5ZZ012022-- 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次而不是一次。这就是为什么我需要小组上班的原因。无法使用组时不良输出的一个示例:

IDforeferid日期创建状态
1ZZ012021-04-23通过了
2ZZ012021-04-23通过了
3ZZ012021-04-23通过了
4ZZ022022-03-24通过了
5ZZ032022-01-12失败的

预期结果:

ID外国创建的状态
1ZZ012021-04-23通过了
2ZZ022022-03-24通过了
3ZZ032022-01-12失败

I have a Table with records like this:

IdForeignIdDate CreatedStatus
1ZZ012021-01-20failed
2ZZ022021-03-24passed
3ZZ012021-08-09passed
4ZZ032022-01-12failed
5ZZ012022-04-23passed

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:

IdForeignIdDate CreatedStatus
1ZZ012021-04-23passed
2ZZ012021-04-23passed
3ZZ012021-04-23passed
4ZZ022022-03-24passed
5ZZ032022-01-12failed

Expected Result:

IdForeignIdDate CreatedStatus
1ZZ012021-04-23passed
2ZZ022022-03-24passed
3ZZ032022-01-12failed

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

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

发布评论

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

评论(1

缪败 2025-02-02 03:40:44

不幸的是,first_value不作为汇总函数可用,而仅作为窗口函数。因此,编译器将其理解为在汇总之后的结果集上运行的。因此,所指的列必须在的组中,但不能放在非聚集和非组列上。

您可以通过汇总函数使用它,

SELECT [SLM].[Id],
       [SLM].[CompanySiteId],
       MAX([QF].[DateSubmitted]) AS [Date Submitted],
       FIRST_VALUE([QF].[Status]) OVER (ORDER BY MAX([QF].[DateSubmitted]) DESC) AS [QC Status]
FROM [dbo].[SiteListMember] AS [SLM]
    JOIN [dbo].[SiteAssessmentStaging] AS [SAS]
        ON [SAS].[SiteListMemberId] = [SLM].[Id]
    JOIN [dbo].[QCForm] AS [QF]
        ON [QF].[SiteAssessmentStagingId] = [SAS].[Id]
WHERE [SAS].[AssessmentTag] = 'Pre-construction' AND [SLM].[CompanySiteId] = 'ABQ00009B'
GROUP BY [SLM].[Id], [SLM].[CompanySiteId], [QF].[Status]
ORDER BY [SLM].[CompanySiteId];

这可能不会给出您想要的结果,如果没有示例数据,很难说。

或者,您需要将其向下推入派生表(子查询)。您可以在整个连接的集合中执行此操作

SELECT t.[Id],
       t.[CompanySiteId],
       MAX(t.[DateSubmitted]) AS [Date Submitted],
       t.[QC Status]
FROM (
    SELECT [SLM].[Id],
           [SLM].[CompanySiteId],
           [QF].[DateSubmitted] AS [Date Submitted],
           FIRST_VALUE([QF].[Status]) OVER (ORDER BY [QF].[DateSubmitted] DESC) AS [QC Status]
    FROM [dbo].[SiteListMember] AS [SLM]
    JOIN [dbo].[SiteAssessmentStaging] AS [SAS]
        ON [SAS].[SiteListMemberId] = [SLM].[Id]
    JOIN [dbo].[QCForm] AS [QF]
        ON [QF].[SiteAssessmentStagingId] = [SAS].[Id]
    WHERE [SAS].[AssessmentTag] = 'Pre-construction' AND [SLM].[CompanySiteId] = 'ABQ00009B'
) t
GROUP BY t.[Id], t.[CompanySiteId], t.[Status]
ORDER BY t.[CompanySiteId];

,也可以通过一个表在一个表格上进行操作,并通过添加条款的分区来加入它。

SELECT [SLM].[Id],
       [SLM].[CompanySiteId],
       MAX([QF].[DateSubmitted]) AS [Date Submitted],
       QC.[QC Status]
FROM [dbo].[SiteListMember] AS [SLM]
    JOIN [dbo].[SiteAssessmentStaging] AS [SAS]
        ON [SAS].[SiteListMemberId] = [SLM].[Id]
    JOIN (
        SELECT *,
          FIRST_VALUE([QF].[Status]) OVER (PARTITION BY [QF].[SiteAssessmentStagingId]
              ORDER BY [QF].[DateSubmitted] DESC) AS [QC Status]
        FROM [dbo].[QCForm] AS [QF]
      ) ON [QF].[SiteAssessmentStagingId] = [SAS].[Id]
WHERE [SAS].[AssessmentTag] = 'Pre-construction' AND [SLM].[CompanySiteId] = 'ABQ00009B'
GROUP BY [SLM].[Id], [SLM].[CompanySiteId], [QF].[Status]
ORDER BY [SLM].[CompanySiteId];

我建议您仅引用需要它的列和表名,并避免使用此类名称。很多[]很烦人。

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 the GROUP BY, but cannot be on non-aggregated and non-grouped columns.

You can use it over an aggregate function

SELECT [SLM].[Id],
       [SLM].[CompanySiteId],
       MAX([QF].[DateSubmitted]) AS [Date Submitted],
       FIRST_VALUE([QF].[Status]) OVER (ORDER BY MAX([QF].[DateSubmitted]) DESC) AS [QC Status]
FROM [dbo].[SiteListMember] AS [SLM]
    JOIN [dbo].[SiteAssessmentStaging] AS [SAS]
        ON [SAS].[SiteListMemberId] = [SLM].[Id]
    JOIN [dbo].[QCForm] AS [QF]
        ON [QF].[SiteAssessmentStagingId] = [SAS].[Id]
WHERE [SAS].[AssessmentTag] = 'Pre-construction' AND [SLM].[CompanySiteId] = 'ABQ00009B'
GROUP BY [SLM].[Id], [SLM].[CompanySiteId], [QF].[Status]
ORDER BY [SLM].[CompanySiteId];

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

SELECT t.[Id],
       t.[CompanySiteId],
       MAX(t.[DateSubmitted]) AS [Date Submitted],
       t.[QC Status]
FROM (
    SELECT [SLM].[Id],
           [SLM].[CompanySiteId],
           [QF].[DateSubmitted] AS [Date Submitted],
           FIRST_VALUE([QF].[Status]) OVER (ORDER BY [QF].[DateSubmitted] DESC) AS [QC Status]
    FROM [dbo].[SiteListMember] AS [SLM]
    JOIN [dbo].[SiteAssessmentStaging] AS [SAS]
        ON [SAS].[SiteListMemberId] = [SLM].[Id]
    JOIN [dbo].[QCForm] AS [QF]
        ON [QF].[SiteAssessmentStagingId] = [SAS].[Id]
    WHERE [SAS].[AssessmentTag] = 'Pre-construction' AND [SLM].[CompanySiteId] = 'ABQ00009B'
) t
GROUP BY t.[Id], t.[CompanySiteId], t.[Status]
ORDER BY t.[CompanySiteId];

Or you can do it just over the one table and join it after, by adding a PARTITION BY clause.

SELECT [SLM].[Id],
       [SLM].[CompanySiteId],
       MAX([QF].[DateSubmitted]) AS [Date Submitted],
       QC.[QC Status]
FROM [dbo].[SiteListMember] AS [SLM]
    JOIN [dbo].[SiteAssessmentStaging] AS [SAS]
        ON [SAS].[SiteListMemberId] = [SLM].[Id]
    JOIN (
        SELECT *,
          FIRST_VALUE([QF].[Status]) OVER (PARTITION BY [QF].[SiteAssessmentStagingId]
              ORDER BY [QF].[DateSubmitted] DESC) AS [QC Status]
        FROM [dbo].[QCForm] AS [QF]
      ) ON [QF].[SiteAssessmentStagingId] = [SAS].[Id]
WHERE [SAS].[AssessmentTag] = 'Pre-construction' AND [SLM].[CompanySiteId] = 'ABQ00009B'
GROUP BY [SLM].[Id], [SLM].[CompanySiteId], [QF].[Status]
ORDER BY [SLM].[CompanySiteId];

I would advise you to only quote column and table names which need it, and to avoid such names if at all possible. Lots of [] is annoying to read.

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