sc1324
sc1324

Reputation: 600

aggregate function help in sql?

If I have the following data:

Policy Number:       Amount:
100                    200
100                    100
101                    50 
102                    90

What can I write in SQL code to get the following result without changing the data

Policy Number:    Amount:
100                 300
101                 50
102                 90

Upvotes: 1

Views: 92

Answers (3)

Scott Austin
Scott Austin

Reputation: 356

Select policy_number, sum(amount) as amount
from table_name
group by policy_number
order by 1

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 191058

SELECT PolicyNumber, SUM(Amount) FROM Table GROUP BY PolicyNumber

Upvotes: 1

Alex K.
Alex K.

Reputation: 175976

You just need to group-by the non-aggregated column like so:

select 
  [Policy Number], 
  sum(Amount)
from the_table
  group by [Policy Number]

Upvotes: 2

Related Questions