我用一種數學運算式進行了這個查詢,但結果不是我所期望的。
查詢是:
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
結果是:
| 名稱 | 柱子 |
|---|---|
| Alimenta??o e Bebidas | 9 |
| Alimenta??o e Bebidas | 20 |
| Alimenta??o e Bebidas | 56 |
| 服務之家 | 6 |
| 康普拉斯 | 9 |
問題是我只想按第一列分組并對第二列求和,但不知何故我不能,因為它給了我這個錯誤:
“SQL 錯誤 [42803]:錯誤:列“d.montante”必須出現在 GROUP BY 子句中或用于聚合函式中”
有誰能夠幫我?
uj5u.com熱心網友回復:
如果您想要按類別劃分的百分比,因為所需的輸出意味著您可以結合 SUM 聚合和 SUM 視窗函式
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/415486.html
標籤:
