views:

732

answers:

4

I have a table of suppliers, and tables Computers, Cameras, Displays all containing the SupplierID field.

I am trying to do a T-SQL that will list all the suppliers, with a count of all rows. I can do them one at a time with:

SELECT SupplierID, COUNT(dbo.Computers.ComputerID) as Computers 
FROM Supplier INNER JOIN
    Computers ON Supplier.SupplierID = Computers.SupplierID
GROUP BY SupplierID

How can I change this to include the other tables - like cameras, displays etc...

A: 

SELECT 
  SupplierID, COUNT(dbo.Computers.ComputerID), COUNT(dbo.Cameras.SupplierID) as Computers 
FROM Supplier 
INNER JOIN Computers ON Supplier.SupplierID = Computers.SupplierID 
INNER JOIN Cameras ON Supplier.SupplierID = Cameras.SupplierID
GROUP BY SupplierID
Hans Malherbe
+1  A: 

Because you have multiple SupplierID columns, I'm surprised your original query worked... The DB engine does not know to use SupplierID from the Supplier or Computers table...

Edit: corrected for OUTER JOINs!

SELECT
    Supplier.SupplierID,
    COUNT(Computers.ComputerID) as Computers,
    COUNT(Displays.DisplayID) as Displays,
    COUNT(Foos.FooID) as Foos,
    COUNT(Bars.BarID) as Bars
FROM
    Supplier
    LEFT JOIN
    Computers ON Supplier.SupplierID = Computers.SupplierID
    LEFT JOIN
    Displays ON Supplier.SupplierID = Displays.SupplierID
    LEFT JOIN
    Foos ON Supplier.SupplierID = Foos.SupplierID
    LEFT JOIN
    Bars ON Supplier.SupplierID = Bars.SupplierID
GROUP BY
    Supplier.SupplierID
gbn
+4  A: 

I don't want to repeat with others have posted, but I agree that you join all the tables and do the counts.

the thing you need to consider (and this is a individual schema consideration) is that you may need to left join the tables. If you inner join them, you may lose rows if you have NULLs in other fields. These fields would be optional fields to the table.

Frank V
+3  A: 

Assuming that a suppliers may not supply each type of product, you will want to use outer joins. If you use inner joins you will only get back suppliers that supply at least one of each production. You also want to count the distinct ProductId for each product type. Otherwise you will get a mulitplication affect. (For example Supplier 1 provides Computers 1 & 2 and Displays 10 & 11, you will get back four rows of Computer 1 Display 10, Computer 1 Display 11, Computer 4 and Display 11.)

Building on gbn's answer:

SELECT
    Supplier.SupplierID,
    COUNT(distinct Computers.ComputerID) as Computers,
    COUNT(distinct Displays.DisplayID) as Displays,
    COUNT(distinct Foos.FooID) as Foos,
    COUNT(distinct Bars.BarID) as Bars
FROM Supplier
LEFT OUTER JOIN Computers 
    ON Supplier.SupplierID = Computers.SupplierID
LEFT OUTER JOIN Displays 
    ON Supplier.SupplierID = Displays.SupplierID
LEFT OUTER JOIN Foos 
    ON Supplier.SupplierID = Foos.SupplierID
LEFT OUTER JOIN Bars 
    ON Supplier.SupplierID = Bars.SupplierID
GROUP BY
    Supplier.SupplierID
Shannon Severance
Thanks - that was it! it is working great now!
aSkywalker