Nat95 Nat95 - 4 months ago 9
SQL Question

Combine colums from table return multiple times the same record

I have 2 tables

INVENTORY
and
PRODUCTS
and I want to select some columns of the
INVENTORY
and one from the
PRODUCTS
. I wrote:

SELECT INVENTORY.product, INVENTORY.version, PRODUCTS.customer
FROM INVENTORY
INNER JOIN PRODUCTS ON
INVENTORY.product = PRODUCTS.product AND INVENTORY.version= PRODUCTS.version


and I get multiple times the same records.. How I fix it?

Answer

Simple distinct or group by:

SELECT DISTINCT INVENTORY.product, INVENTORY.version, PRODUCTS.customer
FROM INVENTORY
INNER JOIN PRODUCTS ON
INVENTORY.product = PRODUCTS.product AND INVENTORY.version= PRODUCTS.version

Or :

SELECT INVENTORY.product, INVENTORY.version, PRODUCTS.customer
FROM INVENTORY
INNER JOIN PRODUCTS ON
INVENTORY.product = PRODUCTS.product AND INVENTORY.version= PRODUCTS.version
GROUP BY INVENTORY.product, INVENTORY.version, PRODUCTS.customer