I have 2 tables namely Inventory and Product. I'd like to SUM up the quantity of all branches for each product while retrieving information from both tables. I'd like to get table RESULT.
I have this but it's not working.
SELECT SUM(T1.Quantity), T1.Barcode, T2.Description, T2.Image
FROM Inventory T1
LEFT OUTER JOIN Product T2 ON T1.Barcode = T2.Barcode
WHERE T1.Quantity > 0
GROUP BY T1.Barcode, T2.Description, T2.Image
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
If I remove the image reference it works but I need the image.
Inventory
| branch | quantity | barcode |
|---|---|---|
| Burgos | 12 | 453011 |
| Rizal | 10 | 453011 |
| Burgos | 5 | 453000 |
Product
| barcode | description | image |
|---|---|---|
| 453011 | Celphone | image |
| 453000 | Battery | image |
RESULT
| barcode | quantity | description | image |
|---|---|---|---|
| 453011 | 22 | Celphone | image |
| 453000 | 5 | Battery | image |
Do the summation as a subquery, then join that to the products table:
nb: Normally it shouldn't be possible to have inventory of products that aren't in the products table, so an inner join should be appropriate. However you are free to use left join if you believe there are mismatches.