Click here to Skip to main content
15,916,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two tables A and B. Both tables have 3 fields. 2 fields in both tables are same as one is 'Article ID' and other is 'Cost'. These 2 fields are common in both tables and data is also common. but the third field in table A is 'qty_should_be' and in table B is 'qty_present'. I want the difference between both fields that how much stock is less or more than it should be

What I have tried:

table A
ART0000024 180.00 100.00
ART0000024 1079.00 1000.00

table B
ART0000024 144 100.00
ART0000024 1099 1000.00
Posted
Updated 7-Mar-18 0:28am

Try:
SQL
SELECT a.[Article ID], a.qty_should_be - b.qty_present
FROM TableA a
JOIN TableB b ON a.[Article ID] = b.[Article ID]
 
Share this answer
 
Try this

DECLARE @A TABLE
(
ARTICLE_ID VARCHAR(10),
COST NUMERIC(10, 2),
qty_should_be INT
)

DECLARE @B TABLE
(
ARTICLE_ID VARCHAR(10),
COST NUMERIC(10, 2),
qty_present INT
)

INSERT INTO @A(ARTICLE_ID, COST, qty_should_be)
SELECT 'ART0000024', 180,	100
UNION ALL
SELECT 'ART0000024', 1079,	1000

INSERT INTO @B(ARTICLE_ID, COST, qty_present)
SELECT 'ART0000024', 144,	100
UNION ALL
SELECT 'ART0000024', 1099,	1000

SELECT A.Article_ID, Sum(qty_should_be) - Sum(Qty_Present)
FROM @A A
      LEFT OUTER JOIN @B  B ON A.Article_ID = B.Article_ID
group by A.Article_ID
 
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