Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在公共表表达式(CTE)中执行SUM()函数_Sql_Sql Server_Tsql_Common Table Expression - Fatal编程技术网

Sql 如何在公共表表达式(CTE)中执行SUM()函数

Sql 如何在公共表表达式(CTE)中执行SUM()函数,sql,sql-server,tsql,common-table-expression,Sql,Sql Server,Tsql,Common Table Expression,我已经多次尝试过了,但它似乎不起作用。这可能吗?以下是我目前的尝试: 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) a

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

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

我假设您使用的是SQL Server,这是由于函数的作用

我不知道你为什么要用CTE。这似乎完全没有必要

我的猜测是,您希望sum(count(RowID))是总的总数,但聚合函数不是这样工作的。GROUPBY子句影响所有聚合函数

我猜这就是你想要的。它使用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

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

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

其次,
groupby
是完全错误的

我认为这基本上就是您想要的查询,假设
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;

给我们看看你的
goes
,你到底希望我们做什么?抱歉:)添加了它。我想这里的逻辑显然会给我整个列的总和作为一个数字,但我希望它显示找到的每个ID的总和……如果这有意义的话。你能详细说明一下“它似乎不起作用”吗?你有错误吗?错误的结果?“打印和”附近的语法不正确。我也不确定语法错误在哪里。如果我对这些部分进行注释,它工作正常,并返回在过去90天内找到的每个ID的月、日、年、ID和每天的打印计数。感谢所有人的帮助-这是一个有假期的长周末,因此我无法对此做出回应/工作。但基本上,我试图找到的结果如下:1。返回每个ID及其每日打印计数,其中其每日打印计数>该ID在过去90天内的平均打印数的3倍。现在,我的查询返回每个ID,月/日/年,以及在过去90天内找到的每个ID的打印计数…问题是我似乎无法获得90天平均值,以查看给定的一天,他们的打印数是平均数的3倍。这更有意义吗?我的最新尝试已被编辑成原始代码。@RoflWaffle17。您已经更改了查询的逻辑。不能在
where
子句中使用聚合函数。删除
中的
,然后回答问题。如果你还有其他需要,那么再问另一个问题。