sql - 在现有查询中添加记录数
问题描述
我有这个查询,它给了我结果
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 条记录
解决方案
使用窗口函数:
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(*)
会使意图更清晰。
推荐阅读
- apache - 我不能使用我的公共 IP 访问我的 xampp 服务器?
- javascript - 用于通过 http 网页聊天应用程序进行推送通知的替代方案
- angular - 迁移到 AngularFire v7 (Firebase v9) - CRUD 操作
- c - LINUX FIFO 命名管道 (IPC) 在特定文件描述符处停止写入/读取消息
- json - 为什么一个 20MB 的 JSON 数组会创建一个 2GB 的表
- javascript - 如何以正确的方式从 javascript 创建一个按钮
- bash - 如何读取 CSV 文件内容并根据 shell 脚本中的字符串值进行查找
- python - 每次循环完成后如何打印字符
- r - 使用甘特图(或类似图)在 R 中绘制具有重叠的隶属关系数据
- shell - CSV 文件重复标题行