如何在sql中跨两个独立的列执行聚合?

xfyts7mz  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(212)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

10个月前关门了。
改进这个问题
我有这样的数据:

Table:
City                 Type

Berlin               Employee
Berlin               Customer
Berlin               Supplier
London               Employee
Madrid               Employee

... 等等
我想知道哪些城市至少有一名员工、客户或供应商。我不确定如何构造聚合函数来实现这一点?非常感谢!

rkue9o1l

rkue9o1l1#

你可以使用聚合函数

select city
    , sum(case when type = 'Employee' then 1 else 0  end) emp
    , sum(case when type = 'Customer' then 1 else 0  end) cust
    , sum(case when type = 'Supplier' then 1 else 0  end) supp
from my_table 
group by  city

检查值(至少一个)

select city
    , sum(case when type = 'Employee' then 1 else 0  end) emp
    , sum(case when type = 'Customer' then 1 else 0  end) cust
    , sum(case when type = 'Supplier' then 1 else 0  end) supp
from my_table 
group by  city

having emp +cust+supp > 0

相关问题