I need a query which returns four different sums based on a column, but don't know how to do it without subqueries.
What I need to do is basically this: I have two tables, "caixa" and "movimentacao". The table "movimentacao" has a field "type", which ranges from 0 to 3, and a field "value", and need a query which returns the sum of value from each type of "movimentacao" belonging to a "caixa", but in a single row, so grouping by the type won't work.
So far my solution is:
SELECT
(SELECT SUM(value) FROM movimentacao where id_caixa = x.id_caixa and type = 0)
(SELECT SUM(value) FROM movimentacao where id_caixa = x.id_caixa and type = 1)
(SELECT SUM(value) FROM movimentacao where id_caixa = x.id_caixa and type = 2)
(SELECT SUM(value) FROM movimentacao where id_caixa = x.id_caixa and type = 3)
FROM caixa x
Is there a way to do this without these four subqueries, using joins instead?