quintroo quintroo - 25 days ago 6
SQL Question

Merge rows with same column value into 1 - Oracle

I have an Oracle table looking like this:

+--+--+------+
|DB|NR|Amount|
+--+--+------+
|BE|10|20 |
+--+--+------+
|BE|10|30 |
+--+--+------+
|FR|20|50 |
+--+--+------+
|FR|20|10 |
+--+--+------+
|DE|30|25 |
+--+--+------+
|BE|35|75 |
+--+--+------+


Which query do I need to use to merge all the rows with the same DB and NR?

This should be the result:

+--+--+------+
|DB|NR|Amount|
+--+--+------+
|BE|10|50 |
+--+--+------+
|FR|20|60 |
+--+--+------+
|DE|30|25 |
+--+--+------+
|BE|35|75 |
+--+--+------+


Thx in advance.

Answer

try this

select DB,NR,sum(amount) from table group by NR,DB