Group and show extra column's distinct values

Viewed 38

How do I get this result? It sums over a and shows distinct values of b.

a b sum
1 9274 83
1 2746 83
1 1847 83
2 6564 83
2 8274 83
2 8567 83

The following query only sums over a

SELECT a, SUM(quantiy) AS sum
FROM table
GROUP BY a
a sum
1 83
2 83
1 Answers

Instead of having a group by clause, you can use the window variant of the sum function and use a partition by clause for it:

SELECT a, b, SUM(quantity) OVER (PARTITION BY a)
FROM   my_table;

SQLFiddle demo

Related