如何在MS Access中为允许GROUP BY和过滤的SQL查询创建百分位函数?

时间:2015-06-17 15:55:45

标签: sql vba ms-access access-vba ms-access-2013

我试图编写一个域名函数,我可以在MS Access SQL视图中使用,如下所示:DMedian in access 2013, no values returned

...但是对于允许你进行GROUP BY和过滤的DPercentile函数。

这是我到目前为止所做的:

 
Public Function DPercentileWithGrpBy( _
 ByVal sFld As String, _
 ByVal sTable As String, _
 ByVal iPercent As Integer, _
 ByVal sGrpByFld As String, _
 ByVal sGrpByValue As String _
 ) As Variant


    Dim db As DAO.Database
    Dim rstDomain As DAO.Recordset
    Dim sSQL As String

    Const errAppTypeError = 3169

    On Error GoTo HandleErr

    Set db = CurrentDb()

    ' Build SQL string for recordset.
    sSQL = "SELECT " & _
                sTable & "." & sGrpByFld & _
                "," & (1 - iPercent / 100) & "*(" & _
                    "SELECT Max(" & sFld & ") " & _
                    "FROM " & sTable & " " & _
                    "WHERE " & sTable & "." & sFld & " IN (" & _
                        "SELECT TOP " & iPercent & " PERCENT " & sFld & " " & _
                        "FROM " & sTable & " " & _
                        "WHERE " & sTable & "." & sGrpByFld & " = " & Chr(34) & sGrpByValue & Chr(34) & " AND " & sFld & " Is Not Null ORDER BY " & sFld & ")) + " & iPercent / 100 & "*(" & _
                            "SELECT Min(" & sFld & ") " & _
                            "FROM " & sTable & " " & _
                            "WHERE " & sTable & "." & sFld & " IN (" & _
                                "SELECT TOP " & 100 - iPercent & " PERCENT " & sFld & " " & _
                                "FROM " & sTable & " " & _
                                "WHERE " & sTable & "." & sGrpByFld & " = " & Chr(34) & sGrpByValue & Chr(34) & " AND " & sFld & " Is Not Null ORDER BY " & sFld & " DESC)" & _
                ") AS " & iPercent & "Percentile " & _
                "FROM " & sTable & " " & _
                "WHERE " & sTable & "." & sGrpByFld & " = " & Chr(34) & sGrpByValue & Chr(34) & " " & _
                "GROUP BY " & sTable & "." & sGrpByFld & ";"
'Debug.Print sSQL

'above should result in something like this:
'SELECT
 ' tblFirst250.[GICS Sector]
'  , 0.75*(
  '  SELECT Max(GM)
  '  FROM tblFirst250
  '  WHERE tblFirst250.GM IN (
   '   SELECT TOP 25 PERCENT GM
   '   FROM tblFirst250
    '  WHERE tblFirst250.[GICS Sector] = "Energy" AND GM Is Not Null ORDER BY GM)) + 0.25*(
     '   SELECT Min(GM)
     '   FROM tblFirst250
     '   WHERE tblFirst250.GM IN (
    '      SELECT TOP 75 PERCENT GM
   '       FROM tblFirst250
  '        WHERE tblFirst250.[GICS Sector] = "Energy" AND GM Is Not Null ORDER BY GM DESC)
 ' ) AS 25Percentile
'FROM tblFirst250
'WHERE tblFirst250.[GICS Sector] = "Energy"
'GROUP BY tblFirst250.[GICS Sector];

    Set rstDomain = db.OpenRecordset(sSQL, dbOpenDynaset)

     DPercentileWithGrpBy = rstDomain

ExitHere:
    On Error Resume Next
    rstDomain.Close
    Set rstDomain = Nothing
    Exit Function

HandleErr:
    ' Return an error value.
    DPercentileWithGrpBy = CVErr(Err.Number)
    Resume ExitHere
End Function

我希望能够将MS Access SQL View中的函数用作查询或查询的一部分。我还需要做一组子记录的百分位数(四分位数)。希望有意义......

编辑:当我调试它并在SQL视图中使用它时,生成的查询有效。

编辑:以下是如何使用它:

DPercentileWithGrpBy( "GM","tblFirst250", 25,"[GICS Sector]","Energy")

0 个答案:

没有答案