Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Selecting top set of random records

Author  Topic 

rmstock
Starting Member

1 Post

Posted - 2004-10-06 : 10:38:35
I'm sure this should be feasible but can't quite get my head round the correct SQL...

I've got a table containing records relating to user tasks and when they were completed. There are the following fields:
TaskID
UserID
Activity
Date Complete

What I want to get out of the table is a random selection of 10 individual tasks completed by each user for each available activity. The data needs to be over a specified date range.

I don't have a problem getting a random list of everything in a given list by ordering my query by userID, activity and NEWID(). What I have a problem with is then reducing that random list down to 10 records per user/activity. Using TOP 10 just leaves me with 10 records total.

Any suggestions gratefully received.
Thanks
Rob Stock

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-10-06 : 10:49:16
will this do:

Select TaskID, UserID, Activity, DateComplete
From myTable as A
Where (Select count(1) From myTable Where TaskID = A.TaskID and UserID = A.UserID DateComplete <= A.DateComplete)<=10
Order By TaskID, UserID, Activity, DateComplete

play with it.

Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -