我正在尝试执行查询以显示按供应商(Supp_Code
)分组的结果,然后在每个组中,结果按排序>佣金代码(Comm_Code
)。
以下查询:
sql = "SELECT [Comm_Code], [Supp_Code], [AqYear], [Product_Code], [Type], [Customer_Code], " & _
"[Commission_Rate], [cType] FROM [Acquisition Commission] GROUP BY [Supp_Code] " & _
"ORDER BY [Comm_Code]"
Dim da As New OleDbDataAdapter(sql, con)
Dim ds As New DataSet
Dim dt As New System.Data.DataTable
da.Fill(ds)
给出错误:
您尝试执行的查询不包含指定表达式“Comm_Code”作为聚合函数的一部分。
我查了一下,this answer说这是因为我将Comm_Code
包含在SELECT
中,而不是GROUP BY
。
所以,我把它改为:
sql = "SELECT [Comm_Code], [Supp_Code], [AqYear], [Product_Code], [Type], [Customer_Code], " & _
"[Commission_Rate], [cType] FROM [Acquisition Commission] GROUP BY [Supp_Code] " & _
"ORDER BY [Comm_Code]"
但后来我得到了同样的错误,但是AqYear
字段。
这将继续下去,直到我在GROUP BY
子句中包含所有选定的字段...肯定这会给我错误的输出,但即使不是,似乎还有很多不必要的查询。
是否有更快/更好的方式来实现我想要做的事情?或者我是否需要在GROUP BY
子句中包含每个查询?
答案 0 :(得分:2)
我认为您只是在寻找带有两个ORDER BY
键的非聚合查询:
SELECT [Comm_Code], [Supp_Code], [AqYear], [Product_Code], [Type], [Customer_Code],
[Commission_Rate], [cType]
FROM [Acquisition Commission]
ORDER BY [Supp_Code], [Comm_Code];
GROUP BY
的查询通常会有COUNT()
或SUM()
等聚合函数。
答案 1 :(得分:0)
-- group by Id & order by Name
SELECT id, name, dob FROM test GROUP BY id ORDER BY name
尝试这样的事情..
干杯!!