我正在尝试计算区别
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 )
我需要获得每个DISTINCT P3
Distinctlist P2 P1 CountOfDistinctInP3
111 , 11, 1, 56pcs.
222, 22, 2, 25pcs.
P3不能为空!!!永远有价值
但是这件好事就像我需要的那样
sql "SELECT DISTINCT `P3`, COUNT(`P3`) FROM [QQ$] GROUP BY `P3` ", Worksheets("QQQQ").Range("a3")
但没有P2和P1((
答案 0 :(得分:0)
尝试以下查询
select count (select distinct p3 from table) from table
答案 1 :(得分:0)
尝试:
SELECT DISTINCT `P3`,`P2`,`P1`, COUNT( `P3`) AS countOf FROM table group by `P3`
答案 2 :(得分:0)
您似乎正在尝试为P1,P2和P3的每个组合值计算P3的不同值的数量。那只会是0(如果P3为NULL)或1.但是使用聚合函数(COUNT)将导致所有行减少到1行,因此P3可能有多个值(因此计数更大),但是P1和P2的值将是未定义的(它们可能来自任何行。
你可能想要的是这样的: -
SELECT `P2`,`P1`, COUNT(DISTINCT `P3`) AS countOf
FROM table
GROUP BY `P2`,`P1`;
或者如果您需要P3的值列表
SELECT `P2`,`P1`, COUNT(DISTINCT `P3`) AS countOf, GROUP_CONCAT(DISTINCT P3)
FROM table
GROUP BY `P2`,`P1`;
SELECT `P3`, `P2`,`P1`, COUNT(`P3`) AS countOf, GROUP_CONCAT(DISTINCT P3)
FROM table
GROUP BY `P3`, `P2`,`P1`;
答案 3 :(得分:0)
解决了,未聚合的SELECT中的每一列都需要在GROUP BY子句中
sql "SELECT `P3`, `P2`, `P1`, COUNT(`P3`) FROM [QQ$] GROUP BY `P3`, `P2`, `P1` ", Worksheets("QQQQ").Range("a3")