要计算我使用此查询的SUM(Count(id))
。我已阅读this
Select *,SUM(Solders) as sums
From
(SELECT
Groups.[ID]
, Groups.[Name]
,Count( Solders.ID) as Solders
FROM Groups join Solders on Groups.ID=Solders.GID
group by Groups.[ID],Groups.[Name]
)B
但发生此错误:
Column 'B.ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
答案 0 :(得分:1)
改为使用窗口函数:
SELECT g.[ID], g.[Name], Count(s.ID) as Solders,
SUM(Count(s.Id)) OVER () as SumCount
FROM Groups g join
Solders s
on g.ID = s.GID
GROUP BY g.[ID], g.[Name];
答案 1 :(得分:0)
这是一个标准组,如下所示:
SELECT ID, NAME,SUM(Solders) as sums
FROM
(SELECT
Groups.[ID]
, Groups.[Name]
,Count( Solders.ID) as Solders
FROM Groups join Solders on Groups.ID=Solders.GID
GROUP BY Groups.[ID],Groups.[Name]
)B
GROUP BY ID, NAME