选择随机用户sql

时间:2014-06-27 17:51:45

标签: sql select random

在我的一个表格中,我有按用户ID,日期,产品,渠道等购买的列表。我想选择一组随机用户,以便我可以运行样本分析。如果我使用rand(),我将有机会将用户的第2次,第3次和第n次购买排除在外。抓住50,100,60000随机用户并查看所有购买内容的最佳方法是什么?

select userID,
date,
SKU,
Campaign,
Device

where date between '2014-05-01' and '2014-05-31'

from Sales

2 个答案:

答案 0 :(得分:0)

这将选择一个顶级(X)用户及其所有销售数据。

declare @samplerate int
set @samplerate = 50

select s.userID,
s.date,
s.SKU,
s.Campaign,
s.Device
from Sales s 
JOIN (select top(@samplerate) RAND() as rnd, x.userID
      from (select distinct userID 
            from Sales) x
      order by rnd) y
   on s.userID = y.userID
where s.date between '2014-05-01' and '2014-05-31'

答案 1 :(得分:0)

DECLARE @UserCount int
SET @UserCount = 50 --Set number of users

;WITH 
UserList AS --Get list of all unique users
(   SELECT DISTINCT UserID
    FROM Sales
),
RndList AS --Get list of # random users
(   SELECT TOP(@UserCount) UserID
    FROM UserList
    ORDER BY NewID()
)
--Get all Sales records for list of users
SELECT s.UserID,s.Date,s.SKU,s.Campaign,s.Device
FROM Sales s
JOIN RndList r ON r.userID = s.UserID
WHERE [Date] BETWEEN '2014-05-01' AND '2014-05-31'