开发者

Can I have the sum() in one select?

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.


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


select A, sum(B) from table
group by A

Should do the trick.


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


Given your description, this will do it for you:

select a, sum(b)
from myTable
group by a


SELECT SUM(B)
FROM (SELECT DISTINCT A, B FROM tbl) PLEASE
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜