0

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'

2 Answers 2

2

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'
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - that's exactly it. For some reason I had it stuck in my head that this was a SUM.
If you'd like to group it by customer and/or order id then you would use a sum to get one row for each group.
-1

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

1 Comment

This is a string concatenation and has nothing to do with arithmetical operations between two columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.