Marllon Nasser
Marllon Nasser

Reputation: 390

Sum specific columns on sql / pl

I have a query that return this:

id | task | company | percent
1  | a    | abc     | 33.20
2  | b    | abc     | 13.70
3  | c    | xpt     | 33.20
4  | d    | xpt     | 20.10

I want to get a query that return something like this:

id | company | percent
1  | abc     | 46.90
3  | xpt     | 53.30

Is it possible? how can I do that?

Upvotes: 0

Views: 52

Answers (2)

Dave
Dave

Reputation: 10924

A simple group by with "min" and "sum" aggregates should work

select 
    min(id) as id, 
    company, 
    sum("percent") as percent 
from 
    [yourtable] 
group by 
    company

As StuartLC noted, "percent" is a reserved word and must be escaped.

Upvotes: 2

StuartLC
StuartLC

Reputation: 107237

You'll need two aggregates with a group by on the non-aggregated company column - one for the MIN(ID) and the other for the SUM(percent)

SELECT MIN(id) AS id,
       company, 
       SUM("percent") AS Percent
FROM SomeTable 
GROUP BY company;

Note that percent is reserved and needs to be escaped. Sql Fiddle here

Upvotes: 2

Related Questions