GROUP BY with Image

87 Views Asked by At

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
1

There are 1 best solutions below

0
Adrian Maxwell On

Do the summation as a subquery, then join that to the products table:

SELECT I.Quantity, I.Barcode, P.Description, P.Image
FROM (
    SELECT SUM(Quantity) Quantity, Barcode
    FROM Inventory
    WHERE Quantity > 0
    GROUP BY Barcode
    ) I
INNER JOIN Product P ON I.Barcode= P.Barcode

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.