【问题标题】:SUM multiple Count and Group bySUM 多个计数和分组依据
【发布时间】:2015-10-23 09:38:27
【问题描述】:

我需要按用户名和帐户名称对总操作 ID(呼叫、会议和任务)组进行计数

我试试这个,但总数不正确

SELECT count(calls.id) + count(meetings.id) + count(tasks.id) AS 'total', users.user_name AS 'name', GROUP_CONCAT(accounts.name) AS 'accounts' 
FROM accounts, calls, users, meetings, tasks
WHERE accounts.id = calls.parent_id 
AND calls.assigned_user_id = users.id
AND accounts.id = meetings.parent_id
AND meetings.assigned_user_id = users.id
AND accounts.id = tasks.parent_id
AND tasks.assigned_user_id = users.id
GROUP BY name

【问题讨论】:

  • 您能给我们展示一些示例数据和预期输出吗?
  • 按名称、accounts.name 分组?
  • 总数怎么不正确?您能否向我们展示示例数据,以及您得到了什么结果?
  • 数据库是 Sugar CRM CE
  • 是不是因为 count(calls.id) 正在计算调用表中的所有 id,因为您没有限制表搜索。如果要限制搜索,请改用表连接。

标签: mysql count sum


【解决方案1】:

如果没有使用具有代表性的数据进行测试,我的猜测是加入的 5 个表使行数相乘,因此总数不正确。在 COUNT() 中使用 DISTINCT 可能会有所帮助,例如

SELECT
      COUNT(DISTINCT calls.id) 
    + COUNT(DISTINCT meetings.id) 
    + COUNT(DISTINCT tasks.id) AS 'total'
    , users.user_name AS 'name'
    , GROUP_CONCAT(DISTINCT accounts.name) AS 'accounts'
FROM accounts
      INNER JOIN calls ON accounts.id = calls.parent_id
      INNER JOIN users ON calls.assigned_user_id = users.id
      INNER JOIN meetings ON accounts.id = meetings.parent_id
                  AND meetings.assigned_user_id = users.id
      INNER JOIN tasks ON accounts.id = tasks.parent_id
                  AND tasks.assigned_user_id = users.id
GROUP BY
      users.user_name
;

请注意,我已经将通过 where 子句加入的旧方法换成了更现代的方法,你真的应该加入。

另一种可能性是您的计数不正确,因为您使用的是 INNER JOINS,这需要两个表中都存在数据才能返回行。所以也许你需要一些左外连接。

SELECT
      COUNT(DISTINCT calls.id) 
    + COUNT(DISTINCT meetings.id) 
    + COUNT(DISTINCT tasks.id) AS 'total'
    , users.user_name AS 'name'
    , GROUP_CONCAT(DISTINCT accounts.name) AS 'accounts'
FROM accounts
      LEFT OUTER JOIN calls ON accounts.id = calls.parent_id
      LEFT OUTER JOIN users ON calls.assigned_user_id = users.id
      LEFT OUTER JOIN meetings ON accounts.id = meetings.parent_id
                  AND meetings.assigned_user_id = users.id
      LEFT OUTER JOIN tasks ON accounts.id = tasks.parent_id
                  AND tasks.assigned_user_id = users.id
GROUP BY
      users.user_name
;

最终的查询可能是连接的混合,一些是 INNER,一些是 LEFT。

【讨论】:

  • 太好了。哪一个有效? (所以当其他人看到这个他们会知道)如果这个答案是正确的,你介意使用复选标记(勾号)来表示它是正确的吗?
  • Natronic 让我知道它是上面看到的 LEFT JOIN 版本。
  • 是的,LEFT JOIN 版本
【解决方案2】:
`SELECT COUNT(calls.id) + COUNT(meetings.id) + COUNT(tasks.id) AS total, GROUP_CONCAT(users.user_name) AS name, GROUP_CONCAT(accounts.name) AS accounts
FROM accounts JOIN calls ON (accounts.id = calls.parent_id) 
JOIN users ON (calls.assigned_user_id = users.id) 
JOIN meetings ON (meetings.assigned_user_id = users.id) 
JOIN tasks ON (accounts.id = tasks.parent_id and tasks.assigned_user_id = users.id) 
GROUP BY users.user_name, accounts.name`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2021-11-04
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    相关资源
    最近更新 更多