Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/68.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 尝试在计数中使用where子句_Sql_Sql Server - Fatal编程技术网

Sql 尝试在计数中使用where子句

Sql 尝试在计数中使用where子句,sql,sql-server,Sql,Sql Server,通过这个语句,我发送了一个过滤器,并希望在计数中使用where过滤器,但我检查了docs,它只允许使用partition和orderby子句 我该怎么做才能修好它 select status, errorid, template, line, error_message, error_type, id , count(*) over (where errorid = 45) as num_rows from errors 以上是语法,因为SQL抛出了一个错误。我想您需要计算error

通过这个语句,我发送了一个过滤器,并希望在计数中使用where过滤器,但我检查了docs,它只允许使用partition和orderby子句

我该怎么做才能修好它

select status, errorid, template, line, error_message, error_type, id
    , count(*) over (where errorid = 45) as num_rows
from errors 

以上是语法,因为SQL抛出了一个错误。

我想您需要计算
errorid=45
。如果是,请使用窗口功能:

select e.*,
       sum(case when errorid = 45 then 1 else 0 end) over () as num_errorid_45
from errors e;

由于您没有发布错误消息,我假设这类似于“关键字“where”附近的语法不正确”

要使其工作,您需要在over子句中放入partitionby子句以获得所需的结果

您可以这样做:

select status, errorid, template, line, error_message, error_type, id
    , count(case when errorid = 45 then 1 else null end) over (partition by errorid) as num_rows
from errors 
如果您愿意,也可以这样做(计数只是每行1的总和)


示例数据和所需结果将有所帮助。错误是什么?获取错误
选择e.*,并将错误e>[42000][Microsoft][SQL Server本机客户端11.0][SQL Server]中的num_errorid_45作为sum(当errorid=45时为1,否则为0结束)列“errors.id”在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。(8120)
@user13895102。这应该是一个窗口函数。但我需要一个计数,而不是1和0。在这种情况下,当where子句used@user13895102 . . . 这将生成与条件匹配的行数。你跑了吗?
select status, errorid, template, line, error_message, error_type, id
    , sum(case when errorid = 45 then 1 else 0 end) over (partition by errorid) as num_rows
from errors