Revokez
Revokez

Reputation: 323

Summing Two Columns with a Join in SQL Server

I'm having a bit of issue trying to sum two column together with a left join - running in to grouping problems.

An example of the issue is below:

Table One: [Order]

ID  CustomerID      
1   512         
2   317         
3   562     

Table Two: [OrderEntry]

OrderID     Type    ID  QuantitySold    QuantityReturned
1           A       1   1               0
1           A       2   3               0
1           A       3   1               1
2           A       4   1               1
3           B       5   2               0

What I'm trying to display:

CustomerID  ID  Sold - Returned     
512         1   1       
512         1   3       
512         1   0       
317         2   0   

Where [OrderEntry].Type = 'A'

Upvotes: 0

Views: 65

Answers (2)

Abhishek Gurjar
Abhishek Gurjar

Reputation: 7476

Here you can use any join as you are using and use concat function on two of your column like this

select concat(OrderEntry.QuantitySold, OrderEntry.QuantityReturned) AS newcolumn_name

Upvotes: -1

Kamil Gosciminski
Kamil Gosciminski

Reputation: 17137

This is very basic SQL:

SELECT
    ord.CustomerID
  , ord.ID
  , orden.QuantitySold - orden.QuantityReturned AS [Sold - Returned]
FROM Order ord
LEFT JOIN OrderEntry orden 
  ON ord.ID = orden.ID
WHERE orden.Type = 'A'

Upvotes: 2

Related Questions