select partnerid, count(distinct processId) balance_count
from mipay.operation_log
where date = '20180501' and source = 'api' and operation = 'pay'
group by partnerid and info like "%BANLANCE%" group by partnerid;
结果:
partnerid balance_count
1 11
2 13
select partnerid, count(distinct processId) bankcard_count
from mipay.operation_log
where date = '20180501' and source = 'api' and operation = 'pay'
group by partnerid and info like "%BANKCARD%" group by partnerid;
结果:
partnerid bankcard_count
1 23
2 17
希望通过一条sql得到如下结果
partnerid balance_count bankcard_count
1 11 23
2 17 17
最佳答案
使用case
表达式进行条件聚合:
select partnerid,
count(distinct case when info like "%BANLANCE%" then processId end) balance_count,
count(distinct case when info like "%BANKCARD%" then processId end) bankcard_count
from mipay.operation_log
where date = '20180501' and source = 'api' and operation = 'pay'
group by partnerid
关于mysql - 如何将这两个sql合并为一个sql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51077803/