简体   繁体   English

SQL:基于列值的 Count()

[英]SQL: Count() based on column value

I have a table as follows:我有一个表如下:

CallID   | CompanyID  | OutcomeID
----------------------------------
1234     | 3344       | 36
1235     | 3344       | 36
1236     | 3344       | 36
1237     | 3344       | 37
1238     | 3344       | 39
1239     | 6677       | 37
1240     | 6677       | 37

I would like to create a SQL script that counts the number of Sales outcomes and the number of all the other attempts (anything <> 36), something like:我想创建一个 SQL 脚本来计算销售结果的数量和所有其他尝试的数量(任何 <> 36),例如:

CompanyID  | SalesCount  | NonSalesCount
------------------------------------------
3344       | 3           | 1
6677       | 0           | 2

Is there a way to do a COUNT() that contains a condition like COUNT(CallID WHERE OutcomeID = 36)?有没有办法做一个包含像 COUNT(CallID WHERE OutcomeID = 36) 这样的条件的 COUNT() ?

You can use a CASE expression with your aggregate to get a total based on the outcomeId value:您可以将 CASE 表达式与聚合一起使用,以根据结果outcomeId值获得总计:

select companyId,
  sum(case when outcomeid = 36 then 1 else 0 end) SalesCount,
  sum(case when outcomeid <> 36 then 1 else 0 end) NonSalesCount
from yourtable
group by companyId;

See SQL Fiddle with Demo参见SQL Fiddle with Demo

Something like this:像这样的东西:

SELECT companyId,
  COUNT(CASE WHEN outcomeid = 36 THEN 1 END) SalesCount,
  COUNT(CASE WHEN outcomeid <> 36 THEN 1 END) NonSalesCount
FROM 
  yourtable
GROUP BY 
  companyId

should work -- COUNT() counts only not null values.应该可以工作—— COUNT()只计算非空值。

Yes.是的。 Count doesn't count NULL values, so you can do this: Count 不计算 NULL 值,因此您可以这样做:

select
  COUNT('x') as Everything,
  COUNT(case when OutcomeID = 36 then 'x' else NULL end) as Sales,
  COUNT(case when OutcomeID <> 36 then 'x' else NULL end) as Other
from
  YourTable

Alternatively, you can use SUM, like bluefeet demonstrated.或者,您可以使用 SUM,就像 bluefeet 演示的那样。

SELECT
    companyId, SalesCount, TotalCount-SalesCount AS NonSalesCount
FROM
    (
    select
      companyId,
      COUNT(case when outcomeid = 36 then 1 else NULL end) SalesCount,
      COUNT(*) AS TotalCount
    from yourtable
    group by companyId
    ) X;

Using this mutually exclusive pattern with COUNT(*)将此互斥模式与COUNT(*)

  • avoids a (very small) overhead of evaluating a second conditional COUNT避免评估第二个条件COUNT的(非常小的)开销
  • gives correct values if outcomeid can be NULL如果outcomeid可以为 NULL,则给出正确的值

Using @bluefeet's SQLFiddle with added NULLs使用@bluefeet 的SQLFiddle添加 NULL

Knowing COUNT() and SUM() only count non-null values and the following rule:知道COUNT()SUM()只计算非空值和以下规则:

true or null = true
false or null = null

For fiddling around, you can take Taryn's answer and circumvent CASE altogether in a super-dirty and error-prone way!对于摆弄,您可以采用 Taryn 的答案并以一种超级脏且容易出错的方式完全绕过CASE

select companyId,
  sum(outcomeid = 36 or null) SalesCount,
  sum(outcomeid <> 36 or null) NonSalesCount
from yourtable
group by companyId;

Forget to add an or null and you'll be counting everything!忘记添加一个or null ,您将计算所有内容!

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

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