SQL 按日期获取前 10 条记录 [英] SQL Get Top 10 records by date

查看:56
本文介绍了SQL 按日期获取前 10 条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张满是臭虫的桌子.BugTitle 是页面错误,我还捕获了错误行.我想构建一个 SQL 查询,根据错误标题和错误行选择前 10 个错误.我有这个查询:

I have a table full of bugs. The BugTitle is the page erroring and I also capture the error line. I would like to build an SQL Query that selects the top 10 bugs based on bugtitle and error line. I have this query:

SELECT COUNT(BugTitle) AS BugCount, BugTitle, ErrLine 
FROM Bugs 
WHERE BugDate >= DateAdd(Day, -30, DateDiff(Day, 0, GetDate())) 
GROUP BY BugTitle, ErrLine 
ORDER BY BugCount, ErrLine DESC

但我不确定它是否正确.我很确定我的测试数据只有 1 个发生在同一行上的错误,但这并没有出现在这个查询中.有人可以帮忙吗?

But I'm not sure if it's correct. I'm pretty sure that my test data only has 1 bug that happens on the same line but that's not showing up with this query. Can anyone help?

推荐答案

要获得您可能希望按计数排序的前 10 个最常用的:

To get the top 10 most frequent you probably want to order by the count:

SELECT TOP(10) COUNT(BugTitle) AS BugCount, BugTitle, ErrLine
FROM Bugs
WHERE BugDate >= DateAdd(Day, -30, DateDiff(Day, 0, GetDate()))
GROUP BY BugTitle, ErrLine
ORDER BY COUNT(BugTitle) DESC

这篇关于SQL 按日期获取前 10 条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆