如何将这两个sql合并成一个sql?

ha5z0ras  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(308)
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
n1bvdmb6

n1bvdmb61#

使用 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

相关问题