Ms access MS访问,获取四分位数(百分比)

Ms access MS访问,获取四分位数(百分比),ms-access,ms-access-2010,percentage,quartile,Ms Access,Ms Access 2010,Percentage,Quartile,我需要在查询中调用此函数,该查询按年份和队列对记录(值)进行排序。例如:如果第1年是第1年,队列A是队列A,请为队列A中的值提供最小值、最大值、中位数和四分位数。 你能告诉我如何在查询设计中应用这个函数吗?多谢各位 Public Function GetQuartile( _ ByVal strTable As String, _ ByVal strField As String, _ ByVal bytQuartile As Byte, _ Optional ByVal byt

我需要在查询中调用此函数,该查询按年份和队列对记录(值)进行排序。例如:如果第1年是第1年,队列A是队列A,请为队列A中的值提供最小值、最大值、中位数和四分位数。
你能告诉我如何在查询设计中应用这个函数吗?多谢各位

Public Function GetQuartile( _
  ByVal strTable As String, _
  ByVal strField As String, _
  ByVal bytQuartile As Byte, _
  Optional ByVal bytMethod As Byte, _
  Optional ByVal strFilter As String) _
  As Double

  ' strTable :    Name of the table/query to analyze.
  ' strField :    Name of the field to analyze.
  ' bytQuartile:  Which min/max or median/quartile to calculate.
  ' bytMethod:    Method for calculation of lower/higher quartile.
  ' strFilter:    Optional filter expression.
  '
  ' Returns:
  '   Minimum, maximum, median or upper/lower quartile
  '   of strField of strTable filtered on strFilter.
  '
  ' 2006-03-05. Cactus Data ApS, CPH.


' Reference for methods for calculation as explained here:
'   http://www.daheiser.info/excel/notes/noteh.pdf
' Note: Table H-4, p. 4, has correct data for dataset 1-96 while
'   datasets 1-100 to 1-97 actually are datasets 1-99 to 1-96
'   shifted one column left.
'   Thus, the dataset 1-100 is missing.
'
'   Method 3b is not implemented as no one seems to use it.
'   Neither are no example data given.
'
' Further notes on methods here:
'   http://mathforum.org/library/drmath/view/60969.html
'   http://www.haiweb.org/medicineprices/manual/quartiles_iTSS.pdf
'
' Data must be in ascending order by strField.


' L: Q1, Lower quartile.
' H: Q3, Higher quartile.
' M: Q2, Median.
' n: Count of elements.
' p: Calculated position of quartile.
' j: Element of dataset.
' g: Decimal part of p
'    to be used for interpolation between j and j+1.

' Basic operation.
' Constant values mimic those of Excel's Quartile() function.

' Find median.
Const cbytQuartMedian             As Byte = 2
' Find lower (first) quartile.
Const cbytQuartLow                As Byte = 1
' Find upper (third) quartile.
Const cbytQuartHigh               As Byte = 3
' Find minimum value.
Const cbytQuartMinimum            As Byte = 0
' Find maximum value.
Const cbytQuartMaximum            As Byte = 4

' Define default operation.
Const cbytQuartDefault = cbytQuartMedian

' Quartile calculation methods.

' Step. Mendenhall and Sincich method.
' SAS #3.
' Round up to actual element of dataset.
' L: -Int(-n/4)
' H: n-Int(-n/4)
Const cbytMethodMendenhallSincich As Byte = 1

' Average step.
' SAS #5, Minitab (%DESCRIBE), GLIM (percentile).
' Add bias of one or two on basis of n/4.
' L: (Int((n+1)/4)+Int(n/4))/2+1
' H: n-(Int((n+1)/4)+Int(n/4))/2+1
Const cbytMethodAverage           As Byte = 2

' Nearest integer to np.
' SAS #2.
' Round to nearest integer on basis of n/4.
' L: Int((n+2)/4)
' H: n-Int((n+2)/4)
' Note:
'   Reference contains an error in example data.
'   Dataset 1-100 to 1-97 (is really 1-99 to 1-96!) should read:
'   25  25  24  24
Const cbytMethodNearestInteger    As Byte = 3

' Parzen method.
' Method 1 with interpolation.
' SAS #1.
' L: n/4
' H: 3n/4
Const cbytMethodParzen            As Byte = 4

' Hazen method.
' Values midway between method 1 steps.
' GLIM (interpolate).
' Add bias of 2, don't round to actual element of dataset.
' L: (n+2)/4
' H: 3(n+2)/4
Const cbytMethodHazen             As Byte = 5

' Weibull method.
' SAS #4. Minitab (DECRIBE), SPSS, BMDP.
' Add bias of 1, don't round to actual element of dataset.
' L: (n+1)/4
' H: 3(n+1)/4
Const cbytMethodWeibull           As Byte = 6

' Freund, J. and Perles, B., Gumbell method.
' S-PLUS, R, Excel, Star Office Calc.
' Add bias of 3, don't round to actual element of dataset.
' L: (n+3)/4
' H: (3n+1)/4
Const cbytMethodFreundPerles      As Byte = 7

' Median Position.
' Median unbiased.
' L: (3n+5)/12
' H: (9n+7)/12
Const cbytMethodMedianPosition    As Byte = 8

' Bernard and Bos-Levenbach.
' L: (n/4)+0.4
' H: (3n/4)/+0.6
' Note:
'   Reference claims L to be (n/4)+0.31.
Const cbytMethodBernardLevenbach  As Byte = 9

' Blom's Plotting Position.
' Better approximation when the distribution is normal.
' L: (4n+7)/16
' H: (12n+9)/16
Const cbytMethodBlom              As Byte = 10

' Moore's first method.
' Add bias of one half step.
' L: (n+0.5)/4
' H: n-(n+0.5)/4
Const cbytMethodMoore1            As Byte = 11

' Moore's second method.
' Add bias of one or two steps on basis of (n+1)/4.
' L: (Int((n+1)/4)+Int(n/4))/2+1
' H: n-(Int((n+1)/4)+Int(n/4))/2+1
Const cbytMethodMoore2            As Byte = 12

' John Tukey's method.
' Include median from odd dataset in dataset for quartile.
' L: (1-Int(-n/2))/2
' H: n-(1-Int(-n/2))/2
Const cbytMethodTukey             As Byte = 13

' Moore and McCabe (M & M), variation of John Tukey's method.
' TI-83.
' Exclude median from odd dataset in dataset for quartile.
' L: (Int(n/2)+1)/2
' H: n-(Int(n/2)+1)/2
Const cbytMethodTukeyMM           As Byte = 14

' Additional variations between Weibull's and Hazen's methods, from
'   (i-0.000)/(n+1.00)
' to
'   (i-0.500)/(n+0.00)
'
' Variation of Weibull.
' L: n(n/4-0)/(n+1)
' H: n(3n/4-0)/(n+1)
Const cbytMethodModWeibull        As Byte = 15
' Variation of Blom.
' L: n(n/4-3/8)/(n+1/4)
' H: n(3n/4-3/8)/(n+1/4)
Const cbytMethodModBlom           As Byte = 16
' Variation of Tukey.
' L: n(n/4-1/3)/(n+1/3)
' H: n(3n/4-1/3)/(n+1/3)
Const cbytMethodModTukey          As Byte = 17
' Variation of Cunnane.
' L: n(n/4-2/5)/(n+1/5)
' H: n(3n/4-2/5)/(n+1/5)
Const cbytMethodModCunnane        As Byte = 18
' Variation of Gringorten.
' L: n(n/4-0.44)/(n+0.12)
' H: n(3n/4-0.44)/(n+0.12)
Const cbytMethodModGringorten     As Byte = 19
' Variation of Hazen.
' L: n(n/4-1/2)/n
' H: n(3n/4-1/2)/n
Const cbytMethodModHazen          As Byte = 20


' Define default method to calculate quartiles.
Const cbytMethodDefault = cbytMethodFreundPerles

Static dbs      As DAO.Database
Static rst      As DAO.Recordset

Dim strSQL      As String
Dim lngNumber   As Long
Dim dblPosition As Double
Dim lngPosition As Long
Dim dblInterpol As Double
Dim dblValueOne As Double
Dim dblValueTwo As Double
Dim dblQuartile As Double

' Use default calculation if choice of calculation is outside range.
If bytQuartile > 4 Then
  bytQuartile = cbytQuartDefault
End If
' Use default method if choice of method is outside range.
If bytMethod = 0 Or bytMethod > 20 Then
  bytMethod = cbytMethodDefault
End If

If dbs Is Nothing Then
  Set dbs = CurrentDb()
End If

If Len(strTable) > 0 And Len(strField) > 0 Then
  strSQL = "SELECT [" & strField & "] FROM [" & strTable & "] "
  strSQL = strSQL & "WHERE ([" & strField & "] Is Not Null) "
  If Len(strFilter) > 0 Then
    strSQL = strSQL & "AND (" & strFilter & ") "
  End If
  strSQL = strSQL & "ORDER BY [" & strField & "];"

  Set rst = dbs.OpenRecordset(strSQL)

  With rst
    If Not .EOF = True Then
      If bytQuartile = cbytQuartMinimum Then
        ' No need to count records.
        lngNumber = 1
      Else
        ' Count records.
        .MoveLast
        lngNumber = .RecordCount
      End If
      Select Case bytQuartile
        Case cbytQuartMinimum
          ' Current record is first record.
          ' Read value of this record.
        Case cbytQuartMaximum
          ' Current record is last record.
          ' Read value of this record.
        Case cbytQuartMedian
          ' Locate position of median.
          dblPosition = (lngNumber + 1) / 2
        Case cbytQuartLow
          Select Case bytMethod
            Case cbytMethodMendenhallSincich
              dblPosition = -Int(-lngNumber / 4)
            Case cbytMethodAverage
              dblPosition = (Int((lngNumber + 1) / 4) + Int(lngNumber / 4)) / 2 + 1
            Case cbytMethodNearestInteger
              dblPosition = Int((lngNumber + 2) / 4)
            Case cbytMethodParzen
              dblPosition = lngNumber / 4
            Case cbytMethodHazen
              dblPosition = (lngNumber + 2) / 4
            Case cbytMethodWeibull
              dblPosition = (lngNumber + 1) / 4
            Case cbytMethodFreundPerles
              dblPosition = (lngNumber + 3) / 4
            Case cbytMethodMedianPosition
              dblPosition = (3 * lngNumber + 5) / 12
            Case cbytMethodBernardLevenbach
              dblPosition = (lngNumber / 4) + 0.4
            Case cbytMethodBlom
              dblPosition = (4 * lngNumber + 7) / 16
            Case cbytMethodMoore1
              dblPosition = (lngNumber + 0.5) / 4
            Case cbytMethodMoore2
              dblPosition = (Int((lngNumber + 1) / 4) + Int(lngNumber / 4)) / 2 + 1
            Case cbytMethodTukey
              dblPosition = (1 - Int(-lngNumber / 2)) / 2
            Case cbytMethodTukeyMM
              dblPosition = (Int(lngNumber / 2) + 1) / 2
            Case cbytMethodModWeibull
              dblPosition = lngNumber * (lngNumber / 4) / (lngNumber + 1)
            Case cbytMethodModBlom
              dblPosition = lngNumber * (lngNumber / 4 - 3 / 8) / (lngNumber + 1 / 4)
            Case cbytMethodModTukey
              dblPosition = lngNumber * (lngNumber / 4 - 1 / 3) / (lngNumber + 1 / 3)
            Case cbytMethodModCunnane
              dblPosition = lngNumber * (lngNumber / 4 - 2 / 5) / (lngNumber + 1 / 5)
            Case cbytMethodModGringorten
              dblPosition = lngNumber * (lngNumber / 4 - 0.44) / (lngNumber + 0.12)
            Case cbytMethodModHazen
              dblPosition = lngNumber * (lngNumber / 4 - 1 / 2) / lngNumber
          End Select
        Case cbytQuartHigh
          Select Case bytMethod
            Case cbytMethodMendenhallSincich
              dblPosition = lngNumber - (-Int(-lngNumber / 4))
            Case cbytMethodAverage
              dblPosition = lngNumber - (Int((lngNumber + 1) / 4) + Int(lngNumber / 4)) / 2 + 1
            Case cbytMethodNearestInteger
              dblPosition = lngNumber - Int((lngNumber + 2) / 4)
            Case cbytMethodParzen
              dblPosition = 3 * lngNumber / 4
            Case cbytMethodHazen
              dblPosition = 3 * (lngNumber + 2) / 4
            Case cbytMethodWeibull
              dblPosition = 3 * (lngNumber + 1) / 4
            Case cbytMethodFreundPerles
              dblPosition = (3 * lngNumber + 1) / 4
            Case cbytMethodMedianPosition
              dblPosition = (9 * lngNumber + 7) / 12
            Case cbytMethodBernardLevenbach
              dblPosition = (3 * lngNumber / 4) + 0.6
            Case cbytMethodBlom
              dblPosition = (12 * lngNumber + 9) / 16
            Case cbytMethodMoore1
              dblPosition = lngNumber - (lngNumber + 0.5) / 4
            Case cbytMethodMoore2
              dblPosition = lngNumber - (Int((lngNumber + 1) / 4) + Int(lngNumber / 4)) / 2 + 1
            Case cbytMethodTukey
              dblPosition = lngNumber - (1 - Int(-lngNumber / 2)) / 2
            Case cbytMethodTukeyMM
              dblPosition = lngNumber - (Int(lngNumber / 2) + 1) / 2
            Case cbytMethodModWeibull
              dblPosition = lngNumber * (3 * lngNumber / 4) / (lngNumber + 1)
            Case cbytMethodModBlom
              dblPosition = lngNumber * (3 * lngNumber / 4 - 3 / 8) / (lngNumber + 1 / 4)
            Case cbytMethodModTukey
              dblPosition = lngNumber * (3 * lngNumber / 4 - 1 / 3) / (lngNumber + 1 / 3)
            Case cbytMethodModCunnane
              dblPosition = lngNumber * (3 * lngNumber / 4 - 2 / 5) / (lngNumber + 1 / 5)
            Case cbytMethodModGringorten
              dblPosition = lngNumber * (3 * lngNumber / 4 - 0.44) / (lngNumber + 0.12)
            Case cbytMethodModHazen
              dblPosition = lngNumber * (3 * lngNumber / 4 - 1 / 2) / lngNumber
          End Select
      End Select
      Select Case bytQuartile
        Case cbytQuartMinimum, cbytQuartMaximum
          ' Read current row.
        Case Else
          .MoveFirst
          ' Find position of first observation to retrieve.
          ' If lngPosition is 0, then upper position is first record.
          ' If lngPosition is not 0 and position is not an integer, then
          ' read the next observation too.
          lngPosition = Fix(dblPosition)
          dblInterpol = dblPosition - lngPosition
          If lngNumber = 1 Then
            ' Nowhere else to move.
            If dblInterpol < 0 Then
              ' Prevent values to be created by extrapolation beyond zero from observation one
              ' for these methods:
              '   cbytMethodModBlom
              '   cbytMethodModTukey
              '   cbytMethodModCunnane
              '   cbytMethodModGringorten
              '   cbytMethodModHazen
              '
              ' Comment this line out, if reading by extrapolation *is* requested.
              dblInterpol = 0
            End If
          ElseIf lngPosition > 1 Then
            ' Move to record to read.
            .Move lngPosition - 1
          End If
      End Select
      ' Retrieve value from first observation.
      dblValueOne = .Fields(0).Value

      Select Case bytQuartile
        Case cbytQuartMinimum, cbytQuartMaximum
          dblQuartile = dblValueOne
        Case Else
          If dblInterpol = 0 Then
            ' Only one observation to read.
            If lngPosition = 0 Then
              ' Return 0.
            Else
              dblQuartile = dblValueOne
            End If
          Else
            If lngPosition = 0 Then
              ' No first observation to retrieve.
              dblValueTwo = dblValueOne
              If dblValueOne > 0 Then
                ' Use 0 as other observation.
                dblValueOne = 0
              Else
                dblValueOne = 2 * dblValueOne
              End If
            Else
              ' Move to next observation.
              .MoveNext
              ' Retrieve value from second observation.
              dblValueTwo = .Fields(0).Value
            End If
            ' For positive values interpolate between 0 and dblValueOne.
            ' For negative values interpolate between 2 * dblValueOne and dblValueOne.
            ' Calculate quartile using linear interpolation.
            dblQuartile = dblValueOne + dblInterpol * CDec(dblValueTwo - dblValueOne)
          End If
      End Select
    End If
    .Close
  End With
Else
  ' Reset.
  Set rst = Nothing
  Set dbs = Nothing
End If

''Set rst = Nothing

  GetQuartile = dblQuartile

End Function
公共函数GetQuartile(_
以字符串形式显示的ByVal strTable_
ByVal strField作为字符串_
ByVal bytQuartile作为字节_
可选的ByVal bytMethod作为字节_
可选的ByVal strFilter(作为字符串)_
加倍
'strTable:要分析的表/查询的名称。
'strField:要分析的字段的名称。
'bytQuartile:要计算的最小值/最大值或中位数/四分位数。
‘bytMethod:计算低/高四分位数的方法。
'strFilter:可选的筛选器表达式。
'
“返回:
'最小、最大、中值或上/下四分位数
在strFilter上筛选的strTable的strField。
'
' 2006-03-05. 仙人掌数据ApS,CPH。
'此处解释的计算方法参考:
'   http://www.daheiser.info/excel/notes/noteh.pdf
注:表H-4,第页。4,具有数据集1-96的正确数据,而
“数据集1-100至1-97实际上是数据集1-99至1-96
"左移一栏,。
'因此,缺少数据集1-100。
'
“方法3b没有实施,因为似乎没有人使用它。
“也没有给出示例数据。
'
'关于方法的进一步说明如下:
'   http://mathforum.org/library/drmath/view/60969.html
'   http://www.haiweb.org/medicineprices/manual/quartiles_iTSS.pdf
'
'数据必须按strField的升序排列。
L:Q1,下四分位数。
H:Q3,更高的四分位数。
M:Q2,中位数。
'n:元素计数。
'p:四分位数的计算位置。
'j:数据集的元素。
'g:p的小数部分
'用于j和j+1之间的插值。
"基本操作"。
'常量值模仿Excel的Quartile()函数。
“找到中间值。
常量cbytQuartMedian作为字节=2
'找到较低的(第一个)四分位数。
常量cbytQuartLow为字节=1
'查找上(第三)四分位数。
常量cbytQuartHigh为字节=3
'查找最小值。
常量CBYT最小值为字节=0
'查找最大值。
常量cbytquartmax作为字节=4
'定义默认操作。
常量cbytQuartDefault=cbytQuartMedian
四分位数计算方法。
”“走吧。Mendenhall和Sincich方法。
“SAS#3。
'四舍五入到数据集的实际元素。
'L:-Int(-n/4)
'H:n-Int(-n/4)
常量cbytMethodMendenhallSincich作为字节=1
“平均步幅。
“SAS#5,Minitab(%Description),GLIM(百分位数)。
'在n/4的基础上增加一个或两个偏差。
'L:(Int((n+1)/4)+Int(n/4))/2+1
'H:n-(Int((n+1)/4)+Int(n/4))/2+1
常量cbytMethodAverage为字节=2
'最接近np的整数。
“SAS#2。
'根据n/4四舍五入到最接近的整数。
'L:Int((n+2)/4)
'H:n-Int((n+2)/4)
“注:
'引用在示例数据中包含错误。
“数据集1-100到1-97(实际上是1-99到1-96!)应为:
'   25  25  24  24
常量cbytMethodNearestInteger,字节=3
“帕尔岑法。
'方法1与插值。
“SAS#1。
L:n/4
'H:3n/4
常量cbytMethodParzen作为字节=4
“哈森法。
'方法1步骤之间的中间值。
“闪烁(插入)。
'添加偏差2,不舍入数据集的实际元素。
'L:(n+2)/4
'H:3(n+2)/4
常量cbytMethodHazen作为字节=5
威布尔方法。
“SAS#4。Minitab(DECRIBE),SPSS,BMDP。
'添加偏差1,不舍入数据集的实际元素。
'L:(n+1)/4
'H:3(n+1)/4
常量cbytMethodWeibull作为字节=6
Freund,J.和Perles,B.,Gumbell方法。
S-PLUS、R、Excel、Star Office Calc。
'添加偏差3,不舍入数据集的实际元素。
'L:(n+3)/4
'H:(3n+1)/4
常量cbytMethodFreundPerles作为字节=7
“中间位置。
“中位数没有偏见。
'L:(3n+5)/12
'H:(9n+7)/12
常量cbytMethodMedianPosition作为字节=8
“伯纳德和博斯莱文巴赫。
'L:(n/4)+0.4
'H:(3n/4)/+0.6
“注:
'参考权利要求L为(n/4)+0.31。
常量cbytMethodBernardLevenbach作为字节=9
“Blom的绘图位置。
“当分布为正态时,更好的近似值。
'L:(4n+7)/16
'H:(12n+9)/16
常量cbytMethodBlom,字节=10
摩尔的第一种方法。
'添加半步的偏差。
'L:(n+0.5)/4
'H:n-(n+0.5)/4
常量CBYT1作为字节=11
摩尔的第二种方法。
'在(n+1)/4的基础上增加一个或两个步骤的偏差。
'L:(Int((n+1)/4)+Int(n/4))/2+1
'H:n-(Int((n+1)/4)+Int(n/4))/2+1
常量Cbyt2作为字节=12
“约翰·图基的方法。
'将奇数数据集中的中值包含在四分位数据集中。
'L:(1-Int(-n/2))/2
'H:n-(1-Int(-n/2))/2
常量cbytMethodTukey作为字节=13
摩尔和麦卡比(M&M),约翰·图基方法的变体。
“TI-83。
'从四分位数据集中的奇数数据集中排除中值。
'L:(Int(n/2)+1)/2
'H:n-(Int(n/2)+1)/2
常量cbytMethodTukeyMM,字节=14
“Weibull和Hazen方法之间的其他变化,来自
“(i-0.000)/(n+1.00)
”“对
'(i-0.500)/(n+0.00)
'
威布尔变异。
'L:n(n/4-0)/(n+1)
'H:n(3n/4-0)/(n+1)
常量cbytMethodModWeibull作为字节=15
“Blom的变异。
'L:n(n/4-3/8)/(n+1/4)
'H:n(3n/4-3/8)/(n+1/4)
常量cbytMethodModBlom作为字节=16
“Tukey的变异。
‘L:n(n/4-1/3)/(n+1/3)
'H:n(3n/4-1/3)/(n+1/3)
常量cbytMethodModTukey作为字节=17
“Cunnane的变异。
'L:n(n/4-2/5)/(n+1/5)
'H:n(3n/4-2/5)/(n+1/5)
常量cbytMethodModcUnane作为字节=18
“格林戈滕的变异。
'L:n(n/4-0.44)/(n+0.12)
'H:n(3n/4-0.44)/(n+0.12)
常量cbytMethodModGringorten作为字节=19
“黑森的变异。
“L:n(n/4-1/2)/n
'H:n(3n/4-1/2)/n
常量cbytMethodModHazen作为字节=20
'定义计算四分位数的默认方法。
Const cbytMethodDefault=cbytMethodFreundPerles
作为DAO.Database的静态数据库
静态rst作为DAO.Recordset
作为字符串的Dim strSQL
变暗lngNumber为L
SELECT 
    GroupByField, 
    GetQuartile("YourTable","ValueField",1,"GroupByField='" & [GroupByField] & "'") AS Q1, 
    GetQuartile("YourTable","ValueField",2,"GroupByField='" & [GroupByField] & "'") AS Q2, 
    GetQuartile("YourTable","ValueField",3,"GroupByField='" & [GroupByField] & "'") AS Q3, 
      (Select Avg(ValueField) 
      From YourTable As T 
      Where T.GroupByField = YourTable.GroupByField;) AS 
    AvgValue
  FROM 
    YourTable
  GROUP BY 
    GroupByField;