我有一张包含来自40个不同供应商的约3,000张发票的表格。在MS Access 2013中,我如何从40个供应商中随机选择一条记录?
以下是我尝试的代码,但就像我说的那样,它不会区分供应商代码,并且可以从一个供应商那里得到40个结果:
SELECT TOP 40 Vendors.Field1, Vendors.Vendor, Vendors.[Lgl Name], Vendors.[Pymnt Amt] INTO [Random Vendors]
FROM Vendors ORDER BY Rnd(Field1);
答案 0 :(得分:0)
这Office Support website它给出了一个公式:Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
。
您可以尝试以下方式:
SELECT Vendors.Field1,
Vendors.Vendor,
Vendors.[Lgl Name],
Vendors.[Pymnt Amt]
INTO [Random Vendors]
FROM Vendors
WHERE Vendors.Field1 IN (Int(3000*Rnd(Field1)))
GROUP BY Vendors.Vendor;
这假设Field1是发票号。如果情况并非如此,那么您必须JOIN
您的发票表。
答案 1 :(得分:0)
我假设该表名为invoices
,您有一个名为vendors
的表,每个供应商有一行:
select v.*
(select top 1 i.invoiceid
from invoices as i
where i.vendor = v.vendor
order by rnd(i.invoiceid)
) as random_invoiceid
from vendors as v;
如果您需要发票的详细信息,请将其用作子查询:
select i.*
from (select v.*
(select top 1 i.invoiceid
from invoices as i
where i.vendor = v.vendor
order by rnd(i.invoiceid)
) as random_invoiceid
from vendors as v
) as vi join
invoices as i
on i.invoiceid = vi.random_invoiceid;