I have a simple view in my MSSQL database. It consists of the following fields:
report_id INT
ym VARCHAR -- YYYY-MM
keyword VARCHAR(MAX)
visits INT
I can easily get the first 10 keywords with the following query:
SELECT TOP 10 *
FROM top_keywords
WHERE ym BETWEEN '2010-05' AND '2010-05'
ORDER BY visits DESC
Now that it's getting complicated, I need to get the top 10 records for each report_idin a given date range ( ym BETWEEN @start_date AND @end_date).
How can I get the top 10 for each report_id? I came across suggestions related to using ROW_NUMBER () and RANK (), but was significantly unsuccessful in their implementation.
Skudd source
share