Click here to Skip to main content
15,892,805 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How to calculate sum of 2 values in 2 tables

Example
C#
Table1				            Table2			
Sno	Overallqty	Address		Sno	shiqty	 Airqty	  Address
1	50	       mumbai		1	20	     25	   mumbai
2	30	       chennai		2	12	     10	   chennai
3	80	       delhi		3	50	     30	   delhi


In these example i need to calculate Sum of overallQTY-(Shipty+Airqty)
In output has to show if the Overallqty-(shipqty+airqty)=0 doesnt show in our query else it has to display

Sample output hint
In this example it has to show first 2 lines because third line will be zero overall qty(80-50+30)=0
Posted
Comments
CHill60 11-Dec-15 3:55am    
What have you tried so far?

Try this,

C#
select t1.*,t2.* from table1 t1 inner join table2 t2 on t1.address=t2.address
where (t1.Overallqty - (t2.shiqty + t2.Airqty))>=0	 



Consider that datatype of Overallqty , shiqty, Airqty all are int or decimal and Overallqty - (shiqty + Airqty) must be greater than 0 which satisfied your requirement.
 
Share this answer
 
v5
Comments
Member 12200213 15-Dec-15 8:43am    
Hi,
your query is working. but if the air qty value is zero system is not fetching that particular line
if the values both ship and air present then only output showing suppose if anyone is not there its not displaying
deepankarbhatnagar 15-Dec-15 23:25pm    
just replace >0 to >=0 like below:

.... where (t1.Overallqty - (t2.shiqty + t2.Airqty))>=0

Please see the solution again I have updated the query.
thanks
Member 12200213 16-Dec-15 2:26am    
Hi,
I have modified like below
where(t1.overallqty-(isnull(t2.shipqty,0)+isnull(t2.airqty,0))>=0
now its working fine.
aarif moh shaikh 15-Dec-15 23:44pm    
good Answer . +5.
Member 11414035 12-Jan-16 0:00am    
good Answer . +5.
From what you explained it doesn't look like you need SUM or any aggregate functions.
Try something like-
SQL
SELECT T1.Sno, T1.Overallqty, T1.Address, T2.Sno, T2.shiqty, T2.Airqty, T2.Address
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Sno=T2.Sno
WHERE T1.Overallqty - (T2.shiqty + T2.Airqty)>0


Hope, it helps.
Please let me know if your requirement is somethingelse than this. :)
 
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