SQL Sum with Group By 不接受仅一列
SQL Sum with Group By not accepting only one column
我用一种数学表达式进行了查询,但结果不是我所期望的。
查询是:
SELECT c.nome,round((d.montante / SUM(d.montante) OVER()),2)* 100 FROM dado d
join categoria c on d.categoria_id = c.id where cast(d.tipologia_enum as text)
=:tipologia and extract(year from d.data) in :anos and extract(month from d.data)
in :meses group by c.nome,d.montante
结果是:
nome
column
Alimentação e Bebidas
9
Alimentação e Bebidas
20
Alimentação e Bebidas
56
Casa e Serviços
6
Compras
9
问题是我只想按第一列分组,然后对第二列求和,但不知何故我做不到,因为它给了我这个错误:
"SQL Error [42803]: ERROR: column "d.montante" must appear in the
GROUP BY clause or be used in an aggregate function"
有人可以帮我吗?
如果您想要按类别的百分比,因为所需的输出意味着您可以组合 SUM 聚合和 SUM window 函数
SELECT c.nome, round((SUM(d.montante) / SUM(SUM(d.montante)) OVER()), 2) * 100
FROM dado d
join categoria c on d.categoria_id = c.id
where cast(d.tipologia_enum as text)
=:tipologia and extract(year from d.data) in :anos and extract(month from d.data)
in :meses
group by c.nome
我用一种数学表达式进行了查询,但结果不是我所期望的。
查询是:
SELECT c.nome,round((d.montante / SUM(d.montante) OVER()),2)* 100 FROM dado d
join categoria c on d.categoria_id = c.id where cast(d.tipologia_enum as text)
=:tipologia and extract(year from d.data) in :anos and extract(month from d.data)
in :meses group by c.nome,d.montante
结果是:
nome | column |
---|---|
Alimentação e Bebidas | 9 |
Alimentação e Bebidas | 20 |
Alimentação e Bebidas | 56 |
Casa e Serviços | 6 |
Compras | 9 |
问题是我只想按第一列分组,然后对第二列求和,但不知何故我做不到,因为它给了我这个错误:
"SQL Error [42803]: ERROR: column "d.montante" must appear in the GROUP BY clause or be used in an aggregate function"
有人可以帮我吗?
如果您想要按类别的百分比,因为所需的输出意味着您可以组合 SUM 聚合和 SUM window 函数
SELECT c.nome, round((SUM(d.montante) / SUM(SUM(d.montante)) OVER()), 2) * 100
FROM dado d
join categoria c on d.categoria_id = c.id
where cast(d.tipologia_enum as text)
=:tipologia and extract(year from d.data) in :anos and extract(month from d.data)
in :meses
group by c.nome