Oracle
 sql >> Database >  >> RDS >> Oracle

SQL:inserire righe con valori riepilogati

SELECT
  LEAST(No1, No2) || ':' || GREATEST(No1, No2)     AS set_label,
  No1,
  No2,
  SUM(Amount)                               AS Amount,
  Stamp
FROM
  tbl
GROUP BY
  GROUPING SETS (
    (LEAST(No1, No2), GREATEST(No1, No2), No1, No2, Stamp),
    (LEAST(No1, No2), GREATEST(No1, No2))
  )

http://sqlfiddle.com/#!4/9afd5/18

Sarebbe meglio se ogni riga avesse un identificatore univoco...

http://sqlfiddle.com/#!4/e9e95/1