how to add values from different records?

1

I'm trying to add the value of id 1 and the value2 of id 2 which in this case would equal 220, how do I do this query ?

       ID      VALOR     VALOR2
---------- ---------- ----------
         1        110        100
         2        100        110

This was the select I was trying to do, but it did not work

select valor+valor2 from teste3 where valor = (select valor from teste3 where id = 1 and valor2 = (select valor2 from teste3 where id = 2))
    
asked by anonymous 08.04.2018 / 17:57

1 answer

2
SELECT SUM(x.valor) AS total
  FROM (
    SELECT valor FROM teste3 WHERE id = 1
    UNION ALL
    SELECT valor2 FROM teste3 WHERE id = 2
  ) x;
    
08.04.2018 / 18:22