Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I CAN'T CONECT TWO COLUMNS ITS SIMPLE AN JUST SIMPLE SOLUTION
WHERE I WRONG?

My two tables are
vp_kalkulacija_roba
and
vp_faktura_roba


From that two tables have same code (sifra) and name (naziv), this
Nab. (kol.)	Prod. (kol.)	Stanje (kol.)
column are counting

Problem is somen fields are null with name and code result query ( they not emtpy)
Query result need instead null to be values

sifra	  naziv	              Nab. (kol.)	Prod. (kol.)	Stanje (kol.)
NULL	    SPANNBOLZEN 2 ZSU-400	 0.00	        1.00	    -1.00
NULL	    SPANNBOLZEN AD40 X 18,5	 0.00	        50.00	    -50.00
72096099	  NULL	                 20.00	        0.00	     20.00
800-40962	  NULL	                  7.00	        0.00	      7.00
800-40964	  NULL	                  3.00	        0.00	     3.00
800-40971	  NULL	                 14.00	        0.00	    14.00


What I have tried:

C#
select x1.sifra, x2.naziv, (CONVERT(decimal(16, 2), ISNULL(x1.Nabavka, 0))) as 'Nab. (kol.)', SUM(CONVERT(decimal(16, 2), ISNULL(x2.Prodaja, 0))) as 'Prod. (kol.)', 
CONVERT(decimal(16, 2), ISNULL(x1.Nabavka, cast(0 as decimal(3,2))) - ISNULL(x2.Prodaja, cast(0 as decimal(3,2)))) as 'Stanje (kol.)' 
from(select naziv, sifra, sum(kolicina) as Nabavka 
from(
select roba as naziv, sifra, convert(float, kolicina) as kolicina from vp_kalkulacija_roba where id_fakture IN(select id from dbo.vp_kalkulacija_lista where magacin = (select vp_magacin from dbo.podaci_o_korisniku)) 
) a group by naziv, sifra) x1 
FULL join(select naziv, sifra, sum(kolicina) as Prodaja 
from(
select roba as naziv, sifra, convert(float, kolicina) as kolicina from vp_faktura_roba where id_fakture IN(select id from vp_faktura_lista where magacin = (select vp_magacin from dbo.podaci_o_korisniku)) 
) b 
group by naziv, sifra) x2 
on  x1.naziv = x2.naziv 
group by x1.sifra, x2.naziv, x1.Nabavka, x2.Prodaja
Posted
Updated 11-Oct-22 23:19pm
v2
Comments
Member 8428760 10-Oct-22 20:14pm    
select a.sifra, b.nazif
from dbo a, vp_kalkulcija_roba b
where a.column = b.column
CHill60 12-Oct-22 4:59am    
That's a very old-school join definition that limits you to using INNER JOIN only - better practice is to use ANSI-92 standards and explicit joins i.e.
select a.sifra, b.nazif from dbo a inner join vp_kalkulcija_roba b on a.column = b.column
and leave the where clause to filter conditions rather than join conditions.
Richard Deeming 11-Oct-22 6:29am    
Click the green "Improve question" link and update your question to include: the structure of your tables; some sample source data from both tables; an explanation of how the tables are related, or an explanation of how you expect to connect the records from the tables if they're not related; and a clear and complete description of what you have tried and where you are stuck.

1 solution

I strongly advise you to start again with this query - you have nested sub-queries four levels deep! Even if you eventually get the right results, performance is going to be abysmal.

Take this lowest level section of code :
SQL
select id from dbo.vp_kalkulacija_lista where magacin = (select vp_magacin from dbo.podaci_o_korisniku))
That could be replaced with a simple inner join e.g.
SQL
select id from dbo.vp_kalkulacija_lista a 
inner join dbo.podaci_o_korisniku b on a.magacin = b.vp_magacin
You can then look again at that simplified section which is now
SQL
select roba as naziv
	,sifra
	,convert(float, kolicina) as kolicina 

from vp_kalkulacija_roba 
where id_fakture IN
(
			select id from dbo.vp_kalkulacija_lista a inner join dbo.podaci_o_korisniku b on a.magacin = b.vp_magacin 
)
Which could now be written as
SQL
select roba as naziv
	,sifra
	,convert(float, kolicina) as kolicina 
from vp_kalkulacija_roba kr
inner join dbo.vp_kalkulacija_lista a on a.id = kr.id_fakture
inner join dbo.podaci_o_korisniku b on a.magacin = b.vp_magacin
I'm not going to continue without sample data and table schemas but hopefully you get the idea.

Another reason your code is difficult to read is all those CONVERT functions - make sure you are storing data in the correct format on the database first, but if you do have to convert it, do that at the last minute. Also - make use of whitespace to make your code more readable, rather than just a single stream of characters.
 
Share this answer
 

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