I have a table with rows that have a number of clicks for a category, for a specific day. Like this:
ID CatID Date Count
1 DADB 2011-11-04 00:00:00 3
2 DCBC 2011-11-04 00:00:00 9
3 DADH 2011-11-04 00:00:00 9
So to display the total count for each of the categories between two dates I would do something like this:
SELECT WebClicksCategory.CatID, sum(WebClicksCategory.Count) as Count
FROM [Web].[dbo].[WebClicksCategory]
WHERE Date >= '2011-04-17 00:00:00.000'
AND Date <= '2012-04-17 00:00:00.000'
GROUP BY WebClicksCategory.CatID
ORDER BY Count desc
However if I want to paginate these results and do something like this:
SELECT * FROM
( SELECT row_number() OVER (order by Count) AS rownum,
WebClicksCategory.CatID, sum(WebClicksCategory.Count) as Count
FROM [Web].[dbo].[WebClicksCategory]
WHERE Date >= '2011-04-17 00:00:00.000'
AND Date <= '2012-04-17 00:00:00.000'
GROUP BY WebClicksCategory.CatID
) AS A
WHERE A.rownum BETWEEN (50) AND (200)
But I get a ‘Column WebClicksCategory.Count is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause’ error. Is there another way to paginate the results?
The problem is not with including the aggregate in the , but actually from including it in the OVER clause:
So what I might suggest is: