|
roych 发表于 2011-3-9 00:37
俺的想法是,先随机抽取5项20%的数据,再据此做联合查询,从而得到每项均20%的数据……
或许子查询也是个 ...
Sub 抽样查询()
Dim ssql As String
Dim n As Long
Dim i As Long
Dim j As Long
Dim str As String
Dim strwh As String
If IsNull(Me.年度.Value) = False And IsNull(Me.月度.Value) = False And IsNull(Me.抽样比率.Value) = False Then
ssql = "DELETE * from 临时表"
CurrentDb.Execute ssql
str = "ID, 流水序列号, 产品单号, 客户姓名, 预估金额, 给付方式, 受理类型, 受理员工号, 申请时间"
For i = 0 To Me.受理类型.ListCount - 1
For j = 0 To Me.受理员工号.ListCount - 1
strwh = "year(申请时间)=" & Me.年度.Value
strwh = strwh & " and month(申请时间)=" & Me.月度.Value
strwh = strwh & " and 受理类型='" & Me.受理类型.Column(0, i) & "'"
strwh = strwh & " and 受理员工号='" & Me.受理员工号.Column(0, j) & "'"
n = Round(DCount("*", "日常数据", strwh) * Me.抽样比率.Value, 0)
If n > 0 Then
ssql = "INSERT INTO 临时表 ( " & str & " ) "
ssql = ssql & "SELECT TOP " & n & " " & str
ssql = ssql & " FROM 日常数据 "
ssql = ssql & "WHERE " & strwh
ssql = ssql & " ORDER BY Rnd([ID])"
CurrentDb.Execute ssql
End If
Next
Next
Else
MsgBox "请选择抽样查询年度、月度、抽样比率!"
End If
End Sub
如果对数量极少的抽样样本,可以考虑将n设置为如下:
n = Int(DCount("*", "日常数据", strwh) * Me.抽样比率.Value) - (DCount("*", "日常数据", strwh) * Me.抽样比率.Value - Int(DCount("*", "日常数据", strwh) * Me.抽样比率.Value) > 0)
这样可以保证最少抽到一条记录。 |
|