有没有办法同时拥有 distinct 子句和 SUM 子句?

Is there a way to have a distinct clause and SUM clause at the same time?

我的数据库中有这个table,它存储了工具应用程序发出的请求的数据。

select * from details

IdDetail | Folio | IdTool | Cost |
1        |53     |917     |137   |
2        |53     |918     |145   |
3        |53     |919     |15    |
4        |54     |917     |137   |
5        |54     |917     |137   |
6        |54     |916     |56    |

它存储请求信息(Folio),工具信息(IdHerramienta and Gasto[Cost])。我的objective是get语句获取details的数据,每行只显示distinct IdHerramienta(IdTool),有多少个是同一个Folio,IdTool获取时的成本总和重复(如果一个工具在同一个作品集上出现多次,每次重复时加上它的成本)

到目前为止我只能显示一个工具被重复了多少次,但我也想知道成本,关于如何获得它有什么想法吗?

select distinct IdTool, count(IdTool) as Quantity 
from details where Folio = 54 group by IdTool

| IdTool | Quantity |
|917     |2         |
|916     |1         |

我想获得的结果

Folio | IdTool | Quantity | Cost  |
54    |917     |2         |274    |
54    |916     |1         |56     |

我认为以下内容符合您的要求:

select idherramienta, count(*), avg(gasto)
from t
where folio = 56
group by idherramienta

您似乎在寻找聚合:

select folio, id_tool, count(*) quantity, sum(cost) cost
from mytable
where folio = 56
group by folio, id_tool