T-SQL return TOP 10 records for each GROUP BY

I have a database table with tracking data from a website that sells tours. Each time a tour is viewed, a simple record is inserted with the date and tourID.

Out of this (big) table, I am using the attached query to look at totals by date using the GROUP BY clause.

I would only like to see the top 10 records for each date...
SELECT     
	CONVERT(varchar(20), datecreated, 101) AS tourDate
	, tourID
	, COUNT(*) AS tourViews

FROM         
	tblWebTour

GROUP BY 
	CONVERT(varchar(20), datecreated, 101)
	, tourID

ORDER BY 
	CONVERT(datetime, CONVERT(varchar(20), datecreated, 101))
	, tourViews DESC

Open in new window

pic001.png
conrad2010Asked:
Who is Participating?
 
Paul_Harris_FusionCommented:
Quick attempt at an example with your code....

Select * from
(
Select tourDate, tourViews, ROW_NUMBER () OVER (PARTITION BY tourDate order by tourDate, tourId) as rn
FROM
(
  SELECT    
      CONVERT(varchar(20), datecreated, 101) AS tourDate
      , tourID
      , COUNT(*) AS tourViews
  FROM        
      tblWebTour

  GROUP BY
      CONVERT(varchar(20), datecreated, 101)
      , tourID
) q1
) q2 where rn < 10
0
 
Paul_Harris_FusionCommented:
Try modifying to use the ROW_NUMBER function
This allows you to allocate row numbers based on a reliable ordering and also allows you to reset the numbering when some value changes.
Use the row_number function in a subquery to generate the row numbers then filter for numbers less than 10 in the outer query

Select * from
(
Select  T.*, ROW_NUMBER() OVER (PARTITION BY X  ORDER BY Y,Z ASC) as mynumber
From MyTable T
) q
where myNumber < 10
0
 
mbarsheshat01Commented:
select top 10 CONVERT(varchar(20), datecreated, 101) AS tourDate ........
0
 
conrad2010Author Commented:
@Paul_Harris_Fusion:

your query works well, but didn't return the top 10 most viewed tours per date...

I added the ORDER BY clause and got an error message returned...


Select * from
(
Select tourDate, tourViews, ROW_NUMBER () OVER (PARTITION BY tourDate order by tourDate, tourId) as rn
FROM
(
  SELECT    
      CONVERT(varchar(20), datecreated, 101) AS tourDate
      , tourID
      , COUNT(*) AS tourViews
  FROM        
      tblWebTour

  GROUP BY
      CONVERT(varchar(20), datecreated, 101)
      , tourID
      ORDER BY
            CONVERT(datetime, CONVERT(varchar(20), datecreated, 101))
            , tourViews DESC
) q1
) q2 where rn < 10


Error:

Msg 1033, Level 15, State 1, Line 19
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
0
 
conrad2010Author Commented:
Nice solution...

I tweaked it a bit in this line:

Select tourDate, tourID, tourViews, ROW_NUMBER () OVER (PARTITION BY tourDate order by tourDate, tourViews DESC) as rn

and got what I was looking for...

Thx!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.