我有一个返回此的查询:
id | task | company | percent
1 | a | abc | 33.20
2 | b | abc | 13.70
3 | c | xpt | 33.20
4 | d | xpt | 20.10
我想获得一个返回类似内容的查询:
id | company | percent
1 | abc | 46.90
3 | xpt | 53.30
有可能吗?我怎么能这样做?
答案 0 :(得分:2)
带有“min”和“sum”聚合的简单group by应该可以正常工作
select
min(id) as id,
company,
sum("percent") as percent
from
[yourtable]
group by
company
正如StuartLC所说,“百分比”是一个保留字,必须转义。
答案 1 :(得分:2)
您需要在非聚合公司列上使用两个聚合组合 - 一个用于MIN(ID),另一个用于SUM(百分比)
SELECT MIN(id) AS id,
company,
SUM("percent") AS Percent
FROM SomeTable
GROUP BY company;
请注意,percent
已保留,需要进行转义。 Sql Fiddle here