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

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

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

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

  1. Table:
  2. City Type
  3. Berlin Employee
  4. Berlin Customer
  5. Berlin Supplier
  6. London Employee
  7. Madrid Employee

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

rkue9o1l

rkue9o1l1#

你可以使用聚合函数

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

检查值(至少一个)

  1. select city
  2. , sum(case when type = 'Employee' then 1 else 0 end) emp
  3. , sum(case when type = 'Customer' then 1 else 0 end) cust
  4. , sum(case when type = 'Supplier' then 1 else 0 end) supp
  5. from my_table
  6. group by city
  7. having emp +cust+supp > 0
展开查看全部

相关问题