简体   繁体   English

这个 SQL 有什么问题?

[英]What's wrong with this SQL?

It's SQL Server:这是 SQL Server:

SQL:查询语句:

SELECT 
    Tb_Supplier.State, Tb_Supplier.City,
    COUNT(DISTINCT Tb_Supplier.Name) as Suppliers
    Tb_Consumer.State, Tb_Consumer.City,
    COUNT(DISTINCT Tb_Consumer.Name) as Consumers
SELECT
      COALESCE(s.state, c.state) AS state
    , COALESCE(s.city, c.city) AS city
    , COALESCE(s.Suppliers, 0) AS Suppliers
    , COALESCE(c.Consumers, 0) AS Consumers
FROM (
      SELECT
            Tb_Supplier.State
          , Tb_Supplier.City
          , COUNT(Tb_Supplier.Name) AS Suppliers
      FROM Tb_Supplier
      GROUP BY
            Tb_Supplier.City
          , Tb_Supplier.State
) AS s
      FULL OUTER JOIN (
            SELECT
                  Tb_Consumer.State
                , Tb_Consumer.City
                , COUNT(Tb_Consumer.Name) AS Consumers
            FROM Tb_Consumer
            GROUP BY
                  Tb_Consumer.City
                , Tb_Consumer.State
      ) AS c ON s.state = c.state
                  AND s.city = c.city

The query above uses what I assume are "master tables" for Suppliers and Consumers, so rows in each should unique define a Supplier or Consumer.上面的查询使用我假设的供应商和消费者的“主表”,因此每个行中的行应该唯一定义供应商或消费者。 Hence count(distinct...) is not required in the above approach.因此,上述方法不需要count(distinct...) A full outer join is used because there may be suppliers in state/city with no matching consumers and vice-versa.使用full outer join是因为州/城市中可能存在没有匹配消费者的供应商,反之亦然。 Due to this coalesce is used in the final select clause to deal with possible NULLs coming from either the supplier or consumer side.由于在最终的 select 子句中使用了这种coalesce来处理来自供应商或消费者方的可能的 NULL。

Why you should stop using commas between tables:为什么你应该停止在表格之间使用逗号:

-- Accidental Cross Join? (Cartesian Product)
-- or is it Deliberate
select * from table_one, table_two

In the above example each row of table_one is multiplied by all the rows of table_two.在上面的例子中,table_one 的每一行都乘以 table_two的所有行 So, if both tables had 100 rows, the result is 100*100 = 10,000 rows.因此,如果两个表都有 100 行,则结果为 100*100 = 10,000 行。 Did I want 10,000 rows?我想要 10,000 行吗?

You have no idea if I wanted it or not, it could be deliberate or just an accident.你不知道我是否想要它,它可能是故意的,也可能只是一个意外。

select * from table_one CROSS JOIN table_two

But now I DO know that the cross join is deliberate.但现在我知道交叉连接是故意的。

Look back at your original question.回头看看你原来的问题。

FROM 
    Tb_Supplier, Tb_Consumer
GROUP BY 
    Tb_Supplier.City, Tb_Supplier.State, Tb_Consumer.City, Tb_Consumer.State​

If you had 1,000 Suppliers and 100,000 consumers how many rows did you create in that from clause?如果您有 1,000 个供应商和 100,000 个消费者,您在 from 子句中创建了多少行? (1,000 * 100,000 = 100,000,000) I'm quite certain that was accidental, and that is why you should stop using commas between tables in the from clause. (1,000 * 100,000 = 100,000,000) 我很确定这是偶然的,这就是为什么你应该停止在 from 子句中的表之间使用逗号。

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

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