0

My query is like this:

select top 50 Empid from (Select top 50 Empid from Emp inner join campus on Emp.CampusId=campus.CampusId join departments on Emp.department=departments.deptid where campus.Cname='Mumbai' ) x order by NEWID();

But Here the number 50 is not constant it is variable(calculted value). So, I want get all the Empids in one datatable and then I need to select few of them randomly (In the above example 50)..Is it possible to do using dataview??

user3116746
  • 41
  • 1
  • 12

2 Answers2

0

This has been answered before.

Is using Random and OrderBy a good shuffle algorithm?

Please see Fisher-Yates shuffle implemented by Jon Skeet.

Basically, randomly shuffle the row numbers or ids, choose little n out of population N.

Community
  • 1
  • 1
CRAFTY DBA
  • 14,351
  • 4
  • 26
  • 30
0

In T-SQL, You can use SELECT TOP (@variable) instead of SELECT TOP 50 as below:

Declare @RecCount int 

--//Set the variable @RecCount here

--//Use it to bring the top record as follows
Select top (@RecCount) Empid 
From Emp inner join campus on Emp.CampusId=campus.CampusId 
               join departments on Emp.department=departments.deptid 
Where campus.Cname='Mumbai'
Order by NEWID();
Kaf
  • 33,101
  • 7
  • 58
  • 78
  • For example if this query returns 400 records, but I need to select only top 200 rows of which top 50 should be shuffled.. – user3116746 Dec 24 '13 at 16:32