J
John Michl
How does one return the average of fields in a VBA sql statement? The
select statement below works fine if pasted into a Query Analyzer window in
SQL Enterprise manager (removing the VBA specific stuff, of course) but does
not work in Excel. If I remove the phrase
AVG(OccurenceSummary.DispenserQty) AS 'Average Dispensers', it works fine
in Excel.
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DRIVER=SQL
Server;SERVER=LINCOLN;UID=JMichl;APP=Microsoft Office
XP;WSID=JMICHL;DATABASE=GPTest;Trusted_Connection=Yes" _
, Destination:=Range("A62"))
.CommandText = Array( _
"SELECT AVG(OccurenceSummary.DispenserQty) AS 'Average Dispensers',
OccurenceSummary.MigrationType, " _
"Count(OccurenceSummary.OccurenceID) AS 'Occurences',
Sum(OccurenceSummary.DispenserQty) AS 'Total Dispensers'" _
& Chr(13) & "" & Chr(10) & "FROM GPTest.dbo.OccurenceSummary
OccurenceSummary" & Chr(13) & "" & Chr(10) & "GROUP BY
OccurenceSummary.MigrationType" )
End With
Thanks
- John Michl
www.JohnMichl.com
select statement below works fine if pasted into a Query Analyzer window in
SQL Enterprise manager (removing the VBA specific stuff, of course) but does
not work in Excel. If I remove the phrase
AVG(OccurenceSummary.DispenserQty) AS 'Average Dispensers', it works fine
in Excel.
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DRIVER=SQL
Server;SERVER=LINCOLN;UID=JMichl;APP=Microsoft Office
XP;WSID=JMICHL;DATABASE=GPTest;Trusted_Connection=Yes" _
, Destination:=Range("A62"))
.CommandText = Array( _
"SELECT AVG(OccurenceSummary.DispenserQty) AS 'Average Dispensers',
OccurenceSummary.MigrationType, " _
"Count(OccurenceSummary.OccurenceID) AS 'Occurences',
Sum(OccurenceSummary.DispenserQty) AS 'Total Dispensers'" _
& Chr(13) & "" & Chr(10) & "FROM GPTest.dbo.OccurenceSummary
OccurenceSummary" & Chr(13) & "" & Chr(10) & "GROUP BY
OccurenceSummary.MigrationType" )
End With
Thanks
- John Michl
www.JohnMichl.com