我将其简化了一点,因为文字数据非常庞大,但是一个非常简单的示例就足够了。我正在研究一个查询,由于海量数据,我希望一次完成一些聚合,而不是许多步骤。
我有两张表
<<customers>> id | first_name | last_name 1 | Reed | Richards 2 | Johnny | Storm 3 | Peter | Parker <<purchases>> id | cid | date 1 | 1 | 2017-01-09 2 | 2 | 2017-01-09 3 | 2 | 2017-01-09 4 | 3 | 2017-01-09
当我运行查询时
SELECT COUNT(c.id) as "Total Customers", COUNT(p.id) as "Total Sales", COUNT(c.id)/COUNT(p.id) as "Sales per customer" FROM test_customers c LEFT OUTER JOIN test_purchases p ON c.id = p.cid
我懂了
4 | 4 | 1
当我寻找…
3 | 4 | 1.3333333...
此示例已大大简化,但实际情况却大得多。我敢肯定有办法做到这一点,我只是不确定现在到底是什么。
您正在尝试计算不重复的行,但不使用 count(distinct ...)
count(distinct ...)
SELECT COUNT(distinct c.id) as "Total Customers", COUNT(distinct p.id) as "Total Sales", COUNT(distinct c.id) * 1.00 / COUNT(distinct p.id) as "Sales per customer" FROM test_customers c LEFT OUTER JOIN test_purchases p ON c.id = p.cid
注意,性能不是很好