从带有Union的MS Access表中选择随机记录

时间:2018-02-21 13:11:20

标签: sql ms-access

我试图从3个不同的组/桶中的Access表中收集随机的记录样本。在我的示例中,我希望每个[呼叫持续时间]存储桶中有50个随机记录,总共150条记录。我使用UNION子句加入每个组。返回的结果不是随机的,每个50个桶只是从1/2/2018的第一个调用日期中提取出来的。它就像TOP子句只是查看表中符合条件的前50条记录,但我想从整个表中获得真正的随机样本。感谢

SELECT * FROM ( 
SELECT TOP 50
 [Workgroup],[Last Name],[First Name],[Titanium Number],
[Phone Number], [Inbound-Outbound], [Date of the Call], [Time of the Call], 
[Duration of the Call], ID 
FROM PCA_Calls WHERE 
([Date of the Call] >=  #1/1/2018# ) AND 
([Date of the Call] <=  #1/31/2018# ) AND 
([Duration of the Call] >= 420) AND ([Duration of the Call] <=900) AND 
([Workgroup] =  "PCA0001A" ) 

UNION 
SELECT TOP 50
 [Workgroup],[Last Name],[First Name],[Titanium Number],
[Phone Number], [Inbound-Outbound], [Date of the Call], [Time of the Call], 
[Duration of the Call], ID 
FROM PCA_Calls WHERE 
([Date of the Call] >=  #1/1/2018# ) AND 
([Date of the Call] <=  #1/31/2018# ) AND 
([Duration of the Call] >= 901) AND ([Duration of the Call] <=1800) AND 
([Workgroup] =  "PCA0001A" ) 

UNION 
SELECT TOP 50
 [Workgroup],[Last Name],[First Name],[Titanium Number],
[Phone Number], [Inbound-Outbound], [Date of the Call], [Time of the Call], 
[Duration of the Call], ID 
FROM PCA_Calls WHERE 
([Date of the Call] >=  #1/1/2018# ) AND 
([Date of the Call] <=  #1/31/2018# ) AND 
([Duration of the Call] >= 1801) AND ([Duration of the Call] <=2700) AND 
([Workgroup] =  "PCA0001A" ) 
) AS Sub ORDER BY rnd(INT(NOW*ID)-NOW*ID);enter code here

1 个答案:

答案 0 :(得分:0)

删除外部随机排序并将其应用于每个查询。

此外,修改表达式以进行排序,如下所示:

Random sorting query Access