Need to sort last column IZLAZ by ROW number
with cteCombined as ( select ROW_NUMBER() OVER (ORDER BY naziv) row_num, naziv as [Naziv], pakovanje [J.M.], '0.00' as [Kolicina], '' as [Izlaz] from dbo.roba_usluge_repromaterijal group by sifra, bar_kod, naziv, pakovanje union all select ROW_NUMBER() OVER (ORDER BY naziv) row_num, naziv as [Naziv], jedinica_mjere [J.M.], sum(kolicina) as [Kolicina], '' as [Izlaz] from dbo.mp_ulazni_racun_repromaterijal_roba where (select redni_broj from dbo.mp_ulazni_racun_repromaterijal_lista where redni_broj IS NOT NULL AND id = id_fakture) IS NOT NULL group by sifra, naziv, jedinica_mjere union all select ROW_NUMBER() OVER (ORDER BY naziv) row_num, naziv as [Naziv], jedinica_mjere [J.M.], sum(kolicina) as [Kolicina], '' as [Izlaz] from dbo.popis_repromaterijal_roba where (select redni_broj from dbo.popis_repromaterijal_lista where redni_broj IS NOT NULL AND id = id_fakture) IS NOT NULL group by sifra, naziv, jedinica_mjere ), cteCombined2 as ( select ROW_NUMBER() OVER (ORDER BY roba) row_num, '' as [Naziv], '' as [J.M.], '0' as [Kolicina], roba as [Izlaz] from dbo.mp_racun_roba where id_fakture IN (select id from mp_racun_lista where datum = '2023-10-01' ) AND roba=(select naziv from dbo.roba_usluge where podgrupa_artikala='HRANA' and roba_usluge.naziv=mp_racun_roba.roba) group by sifra, roba ), cte As ( SELECT row_num, [Naziv], [J.M.],SUM([Kolicina]) AS [Kolicina], [Izlaz] FROM cteCombined GROUP BY row_num,[Naziv], [J.M.], [Izlaz] ), cte2 As ( SELECT row_num, [Naziv], [J.M.], [Kolicina], [Izlaz] FROM cteCombined2 GROUP BY row_num,[Naziv], [J.M.], [Izlaz], [Kolicina] ) select row_num, [Naziv], [J.M.],[Kolicina], [Izlaz] from cte group by row_num, [Naziv], [J.M.], [Kolicina], [Izlaz] union all (select row_num, [Naziv], [J.M.],[Kolicina], [Izlaz] from cte2 group by row_num, [Naziv], [J.M.],[Kolicina], [Izlaz])
Result
row_num Naziv J.M. Kolicina Izlaz 1 AJVAR Kilogram 17.51 2 BIBER Kilogram 2.50 3 BIJELI LUK GRANULE Kilogram 17.25 4 BILJNI SIR Kilogram 37.18 5 BRAŠNO Kilogram 1294.90 6 CVEKLA Kilogram 108.87 7 ÐUVEC Kilogram 90.04 8 EDAMER SIR Kilogram 89.18 9 FAFARONI Kilogram 10.54 10 FIBRETEX 10/1 Kilogram 35.34 11 GLJIVE Kilogram 6.32 12 GRAH Kilogram 114.36 13 GRAŠAK Kilogram 18.11 14 GULAŠ ZACIN BLAGI Kilogram 1.00 14 JAJA Komad 0.00 15 JAJA Komad 510.00 15 JOGURT L Litar 1.30 16 JOGURT L Litar 11.00 16 KOBASICA JUNECA BLAGA Kilogram 6.60 17 JUNECI CEVAP Kilogram 343.97 17 KOBASICA JUNECA LJUTA Kilogram 14.72 18 KOBASICA JUNECA BLAGA Kilogram 72.30 18 KROMPIR Kilogram 23.32 19 KOBASICA JUNECA LJUTA Kilogram 18.00 19 LUK CRVENI Kilogram 88.02 20 KROMPIR Kilogram 665.00 20 MAKARONI Kilogram 1.30 21 LOVOROV LIST Kilogram 0.13 21 MARGARIN Kilogram 0.40 22 LUK CRVENI Kilogram 402.15 22 MESO FAŠIRANO Kilogram 35.45 23 MAKARONI Kilogram 60.80 23 MESO JUNETINA Kilogram 98.45 24 MARGARIN Kilogram 24.75 24 MLIJEKO Litar 30.00 25 MESO FAŠIRANO Kilogram 120.00 25 MRKVA Kilogram 10.83 26 MESO JUNETINA Kilogram 877.16 26 PAPRIKA MLJEVENA Kilogram 4.52 27 MLIJEKO Litar 0.00 27 MRKVA Kilogram 94.99 27 PARADAIZ SOS Litar 0.18 28 MRKVA Kilogram 0.00 28 ORIGANO Kilogram 2.15 28 PARIZER PILECI Kilogram 25.43 29 ORIGANO Kilogram 0.00 29 PALMINO ULJE Litar 20.00 29 PAVLAKA Kilogram 3.26 30 PALMINO ULJE Litar 0.00 30 PAPRIKA MLJEVENA Kilogram 7.50 30 PERŠUNOV LIST Kilogram 0.66 31 PAPRIKA MLJEVENA Kilogram 0.00 31 PARIZER PILECI Kilogram 78.16 31 PILECI BATAK Kilogram 35.65 32 PARADAIZ SOS Litar 0.00 32 PAVLAKA Kilogram 40.55 32 PILECI FILE Kilogram 34.77 33 PARIZER PILECI Kilogram 0.00 33 PILECI BATAK Kilogram 330.23 33 PLJESKAVICA MADI Kilogram 32.00 34 PAVLAKA Kilogram 0.00 34 PILECI FILE Kilogram 420.08 34 POMFRI FROTEZA Kilogram 22.70 35 PERŠUNOV LIST Kilogram 0.00 35 PLJESKAVICA MADI Kilogram 89.00 35 RIBA OSLIC Kilogram 14.57 36 PILECI BATAK Kilogram 0.00 36 POMFRI FROTEZA Kilogram 57.50 36 RIBLJA PAŠTETA Komad 5.00 37 PILECI FILE Kilogram 0.00 37 RIBA OSLIC Kilogram 9.31 37 RIŽA Kilogram 29.80 38 PLJESKAVICA MADI Kilogram 0.00 38 RIŽA Kilogram 138.00 38 SEZONSKA SALATA Kilogram 6.70 39 POMFRI FROTEZA Kilogram 0.00 39 RUSKA SALATA Kilogram 6.50 39 SIRCE ALKOHOLNO Litar 5.00 40 RIBA OSLIC Kilogram 0.00 40 SEZONSKA SALATA Kilogram 679.66 40 SO Kilogram 10.00 41 RIBLJA PAŠTETA Komad 0.00 41 SIRCE ALKOHOLNO Litar 38.00 41 SOS ZA PIZZU Kilogram 12.20 42 RIŽA Kilogram 0.00 42 SO Kilogram 50.00 42 SUDŽUK Kilogram 1.82 43 RUSKA SALATA Kilogram 0.00 43 SOS ZA PIZZU Kilogram 10.00 43 SVJEŽI SIR Kilogram 17.33 44 SEZONSKA SALATA Kilogram 0.00 44 SVJEŽI SIR Kilogram 45.00 44 ŠECER Kilogram 25.00 45 SIRCE ALKOHOLNO Litar 0.00 45 ŠPAGETI Kilogram 52.35 46 SO Kilogram 0.00 46 ŠPINAT Kilogram 1.50 46 ULJE Litar 487.00 47 SOS ZA PIZZU Kilogram 0.00 47 TUNJEVINA Komad 5.00 47 VEGETA Kilogram 55.00 48 SUDŽUK Kilogram 0.00 48 ULJE Litar 88.86 48 VRHNJE ZA KUHANJE Litar 5.85 49 SVJEŽI SIR Kilogram 0.00 49 VEGETA Kilogram 1.69 50 ŠECER Kilogram 0.00 50 VRHNJE ZA KUHANJE Litar 5.95 51 ŠPAGETI Kilogram 0.00 52 ŠPINAT Kilogram 0.00 53 TUNJEVINA Komad 0.00 54 ULJE Litar 0.00 55 VEGETA Kilogram 0.00 56 VRHNJE ZA KUHANJE Litar 0.00 1 0.00 SIS CEVAP 2 0.00 SREDNJI CEVAP 3 0.00 VELIKI CEVAP
Last three column need to be on top
Like this