SQL Get Top 10 records by date

2019-06-24 03:19发布

问题:

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

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?

回答1:

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 tsql