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
 General SQL Server Forums
 New to SQL Server Programming
 Compare two table

Author  Topic 

dhinasql
Posting Yak Master

195 Posts

Posted - 2011-08-24 : 01:52:23
Friends,

Please find my sample table below

table1
-------

PurchaseId | Gross | Deduction | Amount
101 | 1000 | 25 | 975
102 | 2000 | 75 | 1925

table2
-------
PurchaseId | Item | Qty | Price | Amount
101 | A | 2 | 20 | 40
101 | B | 1 | 60 | 60
101 | CXYZ | 1 | 900 | 900
102 | B | 1 | 2000 | 2000

CXYZ is always a special Item.

My Expected output

Where PurchaseId = 101

PurchaseID | Gross | Deduction | SpecialAmount
101 | 1000 | 25 | 900

Where PurchaseId = 102

PurchaseID | Gross | Deduction | SpecialAmount
102 | 2000 | 75 | 0

Thanks in advance.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-24 : 01:57:13
[code]SELECT t1.PurchaseID,
t1.Gross,
t1.Deduction,
COALESCE(t2.Amount,0) AS SpecialAmount
FROM table1 t1
LEFT JOIN table2 t2
ON t2.PurchaseId = t1.PurchaseId
AND t2.Item = 'CXYZ'
[/code]

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

Go to Top of Page

dhinasql
Posting Yak Master

195 Posts

Posted - 2011-08-24 : 02:24:34
Thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-24 : 02:28:50
wc

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

Go to Top of Page
   

- Advertisement -