SQL RANK() equivalent for VBA ADODB.Recordset

邮差的信 提交于 2020-01-15 12:15:24

问题


I'm trying to write an SQL query in VBA for an ADODB.Recordset object that generates a calculated rank field. Using transact-SQL does not work as seen in the sample here.

strSQL = "SELECT DISTINCT [MONTH-YYYY], Location, (SUM([O/T Earnings])/SUM([Monthly Earnings])) AS OT_AS_PCT "
strSQL = strSQL & "RANK() OVER (PARTITION BY [MONTH-YYYY] ORDER BY (SUM([O/T Earnings])/SUM([Monthly Earnings])) DESC) AS RANK "
strSQL = strSQL & "FROM [Overtime Data$] "
strSQL = strSQL & "GROUP BY [MONTH-YYYY], Location "

rst_ranks.Open strSQL, cnn, adOpenStatic, adLockReadOnly

I've noticed when using functions in ADODB before that SQL functions do not work and VBA functions must be used in their place. Is there a VBA Rank() function that allows the PARTITION BY parameter.

Thanks!


回答1:


What's your ADODB Connection provider ?

If you're trying to use Rank() against an Access database it wont work.

Go and see this question Grouped Ranking in Access to see how to do rank with access




回答2:


strangely, standard SQL functions such as RANK() or BETWEEN() do not work in VBA/ADODB record strings. Instead, use of existing VBA functions are necessary and work the same.



来源:https://stackoverflow.com/questions/23614674/sql-rank-equivalent-for-vba-adodb-recordset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!