SQL pull的简单分组

时间:2017-03-28 02:13:22

标签: sql

我有下表:

$A = new Class1();

$Array_1["name1"] = $A;
...
$Array_n["namen"] = $A;
$object_1->field_1 = $A;
...
$object_n->field_n = $A;

$B = new Class2(); // In my case, $B is assigned to a field of $A, and both extend the same abstract class.

TELL_PHP_ENGINE_TO_REPLACE($A, $B);  //Needed operation

我想通过'检查'和每封电子邮件下的计数。像这样:

Check | Email | Count
Y     | a     |  1
Y     | a     |  1
Y     | b     |  1
N     | c     |  1
N     | d     |  1

每次检查'值特定于电子邮件

2 个答案:

答案 0 :(得分:0)

通过将值放在而非中,可以轻松完成此操作。

但它需要两个级别的聚合:

do {
    ...
} while(result < lowerLimit || result > upperLimit);

答案 1 :(得分:0)

这应该可行,但可能有更清洁的方法到达那里。我认为你需要一个额外的聚合层来获取没有电子邮件满足条件的情况,假设你在源表中有一个电子邮件为空的记录。如果源表中没有这些案例的记录,这将不起作用。

select check
      ,count_num
      ,case when email_addresses is null then 0 else email_addresses end as email_addresses
from (
select check, 
       case when count_sum = 1 then 1 when count_sum > 1 then 2+ else 0 end as count_num,
       count(distinct(email)) as email_addresses
group by check, count_num
from (
select check, sum(count) as count_sum, email
from table
group by check, email
)
)