mysql - 如何将这两个sql合并为一个sql?

标签 mysql sql database

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/

相关文章:

c# - 在哪里放置默认值和唯一约束、代码或 SQL 服务器?

database - 如何避免 SQL 注入(inject)攻击?

sql-server - SQL求最大值然后判断哪个名字的值最大

php - 如何使用PHP和mysql数据库在XCode中成功登录后重定向到新 View

c# - ASP.net 找不到连接字符串

C# LINQ 检测插入

c# - 防止 Entity Framework 在使用 Include 时添加 ORDER BY

mysql - 使用前一行的 id 删除多行

mysql - 如何在 IN 子句中使用 max 函数

python - Odo 一个 CSV 文件到 PostgreSQL 数据库