Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query Complexities

Author  Topic 

Yonkouturko
Yak Posting Veteran

59 Posts

Posted - 2012-12-20 : 21:44:32
Can i make a mathematical Expression for two different table..
for example
table1 has fields
id /description /qty
table2 has fields
id description /received_qty

can i do this...
( (select qty from table1) as purchase_QTY
- (minus)
(select received_qty from table2) as received_QTY ) as Remaining_QTY

is that even possible??? Please Help

-----
yonkouTurko

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-12-20 : 22:09:54
[code]select t1.qty - t2.received_qty
from table1 t1 inner join table2 t2 on t1.id = t2.id[/code]

use LEFT JOIN if you may not have the corresponding record in table2


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-21 : 00:43:49
[code]
SELECT id,description,SUM(qty) - SUM(received_qty) AS Remaining
FROM
(
SELECT id,description,qty,CAST(0 AS int) AS received
FROM table1
UNION ALL
SELECT id,description,0,received_qty
FROM table2
)t
GROUP BY id,description
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -