【问题标题】:How to perform SUM() function within a Common Table Expression (CTE)如何在公用表表达式 (CTE) 中执行 SUM() 函数
【发布时间】:2016-05-29 15:10:19
【问题描述】:

我已经多次尝试了,但它似乎不起作用。这可能吗?以下是我目前的尝试:

With PrintsPerDayPerEmp(ID,[Month],[Day],[Year], [Number Of Prints])
AS
(Select ID,
    datepart(month,Agent_Local_Time) as [Month],
    datepart(day,Agent_Local_Time) as [Day],
    datepart(year,Agent_Local_Time) as [Year],
    count(RowID) as [Number of Prints]
From Table1
Where Agent_Local_Time >= (getdate()-90)
Group by datepart(day,Agent_Local_Time),
    datepart(month,Agent_Local_Time),
    datepart(year,Agent_Local_Time),
    ID
)
select *
from PrintsPerDayPerEmp
where [Number Of Prints] > (sum([Number Of Prints])/90)*3
order by ID desc

【问题讨论】:

  • 那么告诉我们你的goes,你希望我们做什么?
  • 抱歉 :) 已添加。
  • 我想这里的逻辑显然会给我整个列的总和作为一个数字,但我希望它显示找到的每个 ID 的总和......如果这有意义的话。跨度>
  • 您能否详细说明“它似乎不起作用”?你有错误吗?错误的结果?
  • “打印总和”附近的语法不正确。我也不确定语法错误在哪里。如果我注释掉这些部分,它可以正常工作并返回过去 90 天内找到的每个 ID 的月、日、年、ID 和每天的打印计数。

标签: sql sql-server tsql common-table-expression


【解决方案1】:

由于getdate() 函数,我假设您正在使用 SQL Server。

我不确定您为什么要尝试使用 CTE。这似乎完全没有必要。

我的猜测是,您期望 sum(count(RowID)) 是总和,但这不是聚合函数的工作方式。 GROUP BY 子句影响所有聚合函数。

我猜这就是你想要的。它使用 OVER() 子句来控制聚合并获得总和:

Select Id,
    datepart(month,Agent_Local_Time) as 'Month',
    datepart(day,Agent_Local_Time) as 'Day',
    datepart(year,Agent_Local_Time) as 'Year',
    count(RowID) as 'Number Of Prints',
    count(RowID) over () as 'Print Sum'
From Table1
Where Agent_Local_Time >= (getdate()-90)
Group by datepart(day,Agent_Local_Time),
    datepart(month,Agent_Local_Time),
    datepart(year,Agent_Local_Time),
    Id
Order by Id desc

或者这个:

Select Id,
    datepart(month,Agent_Local_Time) as 'Month',
    datepart(day,Agent_Local_Time) as 'Day',
    datepart(year,Agent_Local_Time) as 'Year',
    count(RowID) as 'Number Of Prints',
    count(RowID) over (partition by Id) as 'Print Sum'
From Table1
Where Agent_Local_Time >= (getdate()-90)
Group by datepart(day,Agent_Local_Time),
    datepart(month,Agent_Local_Time),
    datepart(year,Agent_Local_Time),
    Id
Order by Id desc

但如果没有一些数据和预期结果,就很难判断。

【讨论】:

  • 感谢所有试图提供帮助的人 - 这是一个有假期的长周末,所以我无法对此做出回应/工作。但基本上,我试图找到的结果如下: 1. 返回每个 ID 及其每日打印计数,其中他们的每日打印计数 > 超过过去 90 天该 ID 平均打印的 3 倍。现在,我的查询正在返回过去 90 天内找到的每个 ID、月/日/年以及每个 ID 的打印计数......问题是我似乎无法获得 90 天的平均值来查看是否为给定一天,他们的打印数量是该平均值的 3 倍。这更有意义吗?
【解决方案2】:

首先,不要对列别名使用单引号。仅对字符串和日期常量使用单引号。在 SQL Server 中,正确的转义字符是双引号或方括号。

第二,group by全错了。

我认为这基本上是您想要的查询,假设 id 是员工 ID:

With PrintsPerDayPerEmp([Id],[Month],[Day],[Year], [Number Of Prints], [Print Sum])
AS (
      Select Id, datepart(month, Agent_Local_Time) as [Month],
             datepart(day, Agent_Local_Time) as [Day],
             datepart(year, Agent_Local_Time) as [Year],
             count(RowID) as [Number of time per Day],
             sum(count(RowID)) over () as [Print Sum]
      From Table1
      Where Agent_Local_Time >= (getdate() - 90)
      Group by datepart(day, Agent_Local_Time),
               datepart(month, Agent_Local_Time),
               datepart(year, Agent_Local_Time),
               Id
     )
select *
from PrintsPerDayPerEmp 
order by Id desc;

【讨论】:

  • 我的最新尝试已被编辑到原始发布的代码中。
  • @RoflWaffle17 。 . .您已更改查询的逻辑。您不能在 where 子句中使用聚合函数。删除where 即可回答问题。如果您有其他需求,请再问一个问题。
猜你喜欢
  • 2016-07-02
  • 2020-12-25
  • 2011-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多