[英]Count group by across multiple tables
I am trying to get individual counts for multiple table join results. 我正在尝试获取多个表连接结果的单个计数。
I have a table of several divisions that should be joined to a table of dealers, which in turn, should be joined to a table of all the transactions by those dealers. 我有几个部门的表,应该将这些部门与一个交易商的表连接起来,而这些表又应该与这些交易商的所有交易的表连接起来。 I want to show how many dealer and transactions there are per division.
我想显示每个部门有多少个经销商和交易。
The following query returns the total number of transactions for both the transactionCount
and the dealerCount
, instead of unique values for each. 以下查询返回
transactionCount
和 dealerCount
的transactionCount
dealerCount
,而不是每个transactionCount
的唯一值。
SELECT
d.*,
COUNT(dlr.id) AS dealerCount,
COUNT(t.id) AS transactionCount
FROM
division AS d
INNER JOIN
dealers AS dlr ON dlr.division = d.id
INNER JOIN
transactions AS t ON t.dealer_code = dlr.dealer_code
GROUP BY
d.id
I haven't tested this, but what about using 我还没有测试过,但是使用
count(distinct dlr.id)
in your select? 在您选择的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.