Answer the question
In order to leave comments, you need to log in
How to correctly get a summing table of two?
Hello, there are two signs.
ПРИХОДЫ
валюта приход
USD 1000
USD 2000
RUB 1500
RUB 500
РАСХОДЫ
валюта расход
USD 100
RUB 300
RUB 2000
ДВИЖЕНИЯ
ВАЛЮТА ПРИХОДОБЩИЙ РАСХОДОБЩИЙ ОСТАТОК
USD 3000 100 2900
RUb 2000 2300 300
Answer the question
In order to leave comments, you need to log in
All currencies are in both tables? Or some of the currencies somewhere may not be?
If there are all types of currencies in INCOMES, then you can do this:
SELECT
p.валюта as ВАЛЮТА,
p.tot as ПРИХОДОБЩИЙ,
r.tot as РАСХОДОБЩИЙ,
(p.tot - r.tot) as ОСТАТОК
FROM
( SELECT SUM(приход) as tot, валюта FROM ПРИХОДЫ GROUP BY валюта ) as p
LEFT JOIN
( SELECT SUM(расход) as tot, валюта FROM РАСХОДЫ GROUP BY валюта ) as r
ON r.валюта = p.валюта
SELECT
p.валюта as ВАЛЮТА,
p.tot as ПРИХОДОБЩИЙ,
r.tot as РАСХОДОБЩИЙ,
(p.tot - r.tot) as ОСТАТОК
FROM
( SELECT SUM(приход) as tot, валюта FROM ПРИХОДЫ GROUP BY валюта ) as p
LEFT JOIN
( SELECT SUM(расход) as tot, валюта FROM РАСХОДЫ GROUP BY валюта ) as r
ON r.валюта = p.валюта
UNION
SELECT
p.валюта as ВАЛЮТА,
p.tot as ПРИХОДОБЩИЙ,
r.tot as РАСХОДОБЩИЙ,
(p.tot - r.tot) as ОСТАТОК
FROM
( SELECT SUM(приход) as tot, валюта FROM ПРИХОДЫ GROUP BY валюта ) as p
RIGHT JOIN
( SELECT SUM(расход) as tot, валюта FROM РАСХОДЫ GROUP BY валюта ) as r
ON r.валюта = p.валюта
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question