Click here to Skip to main content
15,911,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use this code and its ok from 1 table

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


I need to select from multiple tables exmp 3
How to do that?

I try but dont ok

What I have tried:

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
Posted
Updated 16-Apr-20 4:19am

It's not entirely clear what the problem is, but try:
SQL
WITH cteCombined As
(
    SELECT
        iznos_sa_porezom As [Duguje], 
        '0.00' as [Potražuje]
    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]
    FROM
        dbo.mp_ulazni_racun_roba_lista
    WHERE
        redni_broj IS NOT NULL
    GROUP BY
        iznos_sa_porezom
),
cte As
(
    SELECT
        [Duguje], 
        [Potražuje],
        ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS RN 
    FROM
        cteCombined
)
SELECT
    [Duguje], 
    [Potražuje],
    (SELECT SUM([Duguje] - [Potražuje]) FROM cte t2 WHERE t2.RN <= cte.RN) AS Saldo
FROM
    cte
;
Depending on your requirements, you might need to move the GROUP BY from cteCombined to cte.
 
Share this answer
 
Comments
Goran Bibic 16-Apr-20 10:41am    
Thank you
Maciej Los 16-Apr-20 11:12am    
5ed!
This would be a lot easier to work with if there was some sample data and expected results

I've never really had luck when combining the UNION ALL with a GROUP BY, and generally will break it apart into multiple CTEs.
I would start off with something like this and work with what data you have and what results you expect and the work from there.
SQL
; WITH cte1 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
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
	
)
; WITH cte2 AS (
	SELECT   Duguje, Potražuje, RN
	FROM     cte1
	GROUP BY Duguje
)

SELECT Duguje
,      Potražuje
,      Saldo = ( SELECT Sum(Duguje-Potražuje)
                 FROM   cte2 t2
                 WHERE  t2.RN <= cte.RN)
FROM   cte2
 
Share this answer
 
Comments
Maciej Los 16-Apr-20 11:13am    
5ed!
MadMyche 16-Apr-20 11:30am    
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900