【问题标题】:2 COUNTs in one SQL statement with each different conditions (MYSQL)一个 SQL 语句中的 2 个计数,每个不同的条件 (MYSQL)
【发布时间】:2018-07-10 13:58:57
【问题描述】:

我一直在寻找一种方法来做到这一点,但所提供的帮助总是有问题。 (MYSQL不接受,我的条件比提问的人多等)

我希望能够为员工数两件事。我有3个不同的条件。这怎么可能做到??

下面的 COUNT 代码非常错误,但它是为了说明我想要它做什么。我输出 2 行,所以我需要它们有不同的名称。

仅供参考:(它适用于一个 COUNT)

谢谢

SELECT employees.employees_ID,
       employees.name,
       employees.country_count_ID,
       employees.department_ID, 
       employees.initials,
       country.country_initials,

COUNT(distinct clients.retailer_ID when clients.progress_ID = 6) as aRows,
COUNT(distinct case when clients.progress_ID = 7) as bRows

FROM employees

LEFT OUTER JOIN clients ON employees.employees_ID = clients.sales_employees_ID
LEFT OUTER JOIN country ON employees.country_count_ID = country.count_ID

WHERE employees.department_ID = 1

GROUP BY employees.employees_ID,
         employees.name,
         employees.country_count_ID,
         employees.department_ID,
         employees.initials,
         country.country_initials

【问题讨论】:

    标签: php mysql sql count multiple-conditions


    【解决方案1】:

    您可以将casecount(distinct) 一起使用:

    COUNT(distinct case when clients.progress_ID = 6 then clients.retailer_ID  end) as aRows,
    COUNT(distinct case when clients.progress_ID = 7 then clients.retailer_ID  end) as bRows,
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      相关资源
      最近更新 更多