[vba]SELECTArticleViewed,
SUM(CASE WHEN DATEDIFF(Day, DateViewed, Getdate()) between 0 and 10 THEN 1 ELSE 0 END) AS 'D10',
SUM(CASE WHEN DATEDIFF(Day, DateViewed, Getdate()) between 11 and 20 THEN 1 ELSE 0 END) AS 'D20',
SUM(CASE WHEN DATEDIFF(Day, DateViewed, Getdate()) between 21 and 30 THEN 1 ELSE 0 END) AS 'D30'
FROM ArticleViews
Group by ArticleViewed
Order by ArticleViewed desc
[/vba]
Hope this helps. I made some assumptions that may not be right. I assumed:
- Each record in your table represents a single view
- You're using Transact SQL to retrieve the data
- ArticleViewed and DateViewed are columns/fields in your table/file
I didn't see the need for a subselect so maybe I missed something there. I'm puzzled also by the ORDER BY. I suspect you want a grand total and sort by that.