tags:

views:

156

answers:

5

I have e table like this :

A B
1 1.5
1 1.5
2 2.3
2 2.3
2 2.3
3 1.5
3 1.5

how could i make the sum of column B, grouped by in 1.5, 2.3 and 1.5. in few words, I want to group by first and then make the sum(), but in one select.

in this table, if you group by A column the result is:

A B
1 1.5
2 2.3
3 1.5

now i want to sum() the B column.

+1  A: 

I'm not positive, but it looks like you want the sum for each individual value in column B (with column A being ignored completely)

Give this a try:

select b, sum(b)
from tableName
group by b
Justin Niessner
+1  A: 
select A, sum(B) from table
group by A

Should do the trick.

Vincent Ramdhanie
A: 

Given your description, this will do it for you:

select a, sum(b)
from myTable
group by a
Jason
A: 
SELECT SUM(B)
FROM (SELECT DISTINCT A, B FROM tbl) PLEASE
Cade Roux
+1  A: 

This would sum distinct rows after grouping on a and b:

select sum(b)
from (
    select b
    from YourTable
    group by a, b
) sub

This would sum all distinct values of B:

select sum(distinct b)
from YourTable

This would sum the highest values of B for each value of A:

select sum(maxb)
from (
    select max(b) as maxb
    from YourTable
    group by a
) sub
Andomar