首页 > 解决方案 > 在现有查询中添加记录数

问题描述

我有这个查询,它给了我结果

select status,
       count(errorid) as field1,
       errorid,
       template,
       line,
       error_message,
       error_type,
       max(id) as field2 
FROM errors
GROUP BY status,errorid,template,error_message, error_type,line
ORDER BY field1 desc 

我正在尝试实现其中的记录总数,例如上面的查询有 5000 条记录,它应该在单独的列中显示 5000 条,所以我可以使用它,我尝试添加计数,但问题是计算所有 700000 条记录

标签: sqlsql-server

解决方案


使用窗口函数:

select status, count(errorid) as field1, errorid, template, line, error_message, error_type,
       max(id) as field2, count(*) over () as num_rows
from errors
group by status, errorid, template, error_message, error_type,line 
order by field1 desc ;

对我来说有点奇怪,你errorid同时拥有一个字段和一个count(). 如果您正在寻找重复的错误,这是有道理的,但count(*)会使意图更清晰。


推荐阅读