我想算distinctsExcel的VBA ADODB.Connection的Sql計數distincts
Sub sql(sqlStr As String, PasteRnG As Range)
Dim con As Object
Dim rstData As Object
Dim sDatabaseRangeAddress As String
Set con = CreateObject("ADODB.Connection")
Set rstData = CreateObject("ADODB.Recordset")
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ActiveWorkbook.FullName & ";Extended Properties = ""Excel 12.0 Macro;HDR=yes"";"
rstData.Open sqlStr, con
PasteRnG.CopyFromRecordset rstData
rstData.Close
Set rstData = Nothing
Set con = Nothing
End Sub
查詢
sql "SELECT DISTINCT `P3`,`P2`,`P1`, COUNT(DISTINCT `P3`) AS countOf FROM [QQ$] ", Worksheets("QQQQ").Range("a3")
錯誤的
SELECT DISTINCT `P3`,`P2`,`P1`, COUNT(DISTINCT `P3`) AS countOf FROM table;
但我得到COUNT(DISTINCT
P3 )
我需要在每個不同的P3
Distinctlist P2 P1 CountOfDistinctInP3
111 , 11, 1, 56pcs.
222, 22, 2, 25pcs.
第4列數得到P3不能爲空!總是有值
但這精品工程,正如我需要
sql "SELECT DISTINCT `P3`, COUNT(`P3`) FROM [QQ$] GROUP BY `P3` ", Worksheets("QQQQ").Range("a3")
但是,如果沒有P2和P1(((
刪除'distinct'從'計數()' – goseo
我會建議使用'組by',而不是不同的,但我不知道什麼是自己的原始數據,什麼是預期的輸出。一個例子可能有幫助。 –
錯誤您試圖執行不包含指定表達式'P3'作爲agregate函數的一部分的查詢 – user3748026