Выберите значение из нескольких таблиц MS SQL
Я использую этот код и его ОК из 1 таблицы
with cte as ( select iznos_sa_porezom as 'Duguje', '0.00' as 'Potražuje', ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RN from dbo.mp_ulazni_racun_ostalo_lista where redni_broj IS NOT NULL group by iznos_sa_porezom ) select [Duguje], [Potražuje], (SELECT sum(Duguje-Potražuje) FROM cte t2 WHERE t2.RN <= cte.RN) AS Saldo from cte
Мне нужно выбрать из нескольких таблиц exmp 3
Как это сделать?
Я стараюсь, но не могу.
Что я уже пробовал:
with cte as ( select iznos_sa_porezom as 'Duguje', '0.00' as 'Potražuje', ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RN from dbo.mp_ulazni_racun_ostalo_lista where redni_broj IS NOT NULL group by iznos_sa_porezom union all select iznos_sa_porezom as 'Duguje', '0.00' as 'Potražuje', ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RN from dbo.mp_ulazni_racun_roba_lista where redni_broj IS NOT NULL group by iznos_sa_porezom ) select [Duguje], [Potražuje], (SELECT sum(Duguje-Potražuje) FROM cte t2 WHERE t2.RN <= cte.RN) AS Saldo from cte