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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.