简体   繁体   English

如何对多个表使用T-SQL分组依据?

[英]How do I use T-SQL Group By with multiple tables?

I have a table of suppliers, and tables Computers, Cameras, Displays all containing the SupplierID field. 我有一个供应商表,而表“计算机”,“照相机”,“显示”均包含“供应商ID”字段。

I am trying to do a T-SQL that will list all the suppliers, with a count of all rows. 我正在尝试做一个T-SQL,它将列出所有供应商,并统计所有行。 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... 我如何更改此设置以包括其他表格-如相机,显示器等...

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. 您需要考虑的事情(这是一个单独的架构考虑)是您可能需要 left join表。 If you inner join them, you may lose rows if you have NULL s in other fields. 如果您内部加入它们,那么如果其他字段中有NULL ,则可能会丢失行 These fields would be optional fields to the table. 这些字段是表的可选字段。

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. 您还希望为每种产品类型计算不同的ProductId。 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.) (例如,供应商1提供计算机1和2以及显示器10和11,您将获得四行计算机1显示器10,计算机1显示器11,计算机4和显示器11。)

Building on gbn's answer: 基于gbn的答案:

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

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... 因为您有多个SupplierID列,所以很奇怪您的原始查询已起作用...数据库引擎不知道使用Suppliers或Computers表中的SupplierID。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM