SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Random account selection for each staff person


Random account selection for each staff person

Author
Message
adam-639168
adam-639168
Old Hand
Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)

Group: General Forum Members
Points: 332 Visits: 168
The problem is to extract a random sample of 3 accounts for each staff person. In reality, my table has many more staff and accounts but I tried to keep it simplified here.

create table #accounts(staffid int, account int)
insert into #accounts (staffid,account) values (1,9000)
insert into #accounts (staffid,account) values (1,9001)
insert into #accounts (staffid,account) values (1,9002)
insert into #accounts (staffid,account) values (1,9003)
insert into #accounts (staffid,account) values (1,9004)
insert into #accounts (staffid,account) values (1,9005)
insert into #accounts (staffid,account) values (2,8000)
insert into #accounts (staffid,account) values (2,8001)
insert into #accounts (staffid,account) values (2,8002)
insert into #accounts (staffid,account) values (2,8003)
insert into #accounts (staffid,account) values (2,8004)
insert into #accounts (staffid,account) values (2,8005)
insert into #accounts (staffid,account) values (3,7000)
insert into #accounts (staffid,account) values (3,7001)
insert into #accounts (staffid,account) values (3,7002)
insert into #accounts (staffid,account) values (3,7003)
insert into #accounts (staffid,account) values (3,7004)
insert into #accounts (staffid,account) values (3,7005)

Thanks in advance for solutions you may come up with.
adam-639168
adam-639168
Old Hand
Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)Old Hand (332 reputation)

Group: General Forum Members
Points: 332 Visits: 168
Looks like I just figured out my own problem. Here's my solution. Just closing the loop.

SELECT staffid,account FROM
(select
ROW_NUMBER() OVER(PARTITION BY staffid ORDER BY newid() ) AS RowID
, * FROM #accounts ) InnerQ
WHERE InnerQ.RowID <= 3
totalkarachi
totalkarachi
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 3
its for random record http://codingresolved.com/discussion/98/get-random-records-from-sql
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search