gpt4 book ai didi

sql - 如何在Oracle中查找相关值?

转载 作者:行者123 更新时间:2023-12-02 21:54:45 26 4
gpt4 key购买 nike

假设您有一个表,例如:

id   foreign_key  status
------------------------
1 1 new
2 1 incative
3 1 approved
4 2 new
5 2 new
6 2 approved
7 3 new
8 3 approved
9 4 approved

如何查找对于给定的foreign_key只有一条记录处于新状态而其他记录已批准的记录,就像foreign_key 3的情况一样?

最佳答案

select foreign_key from table
group by foreign_key
having
abs(1 - count(case status when 'new' then 1 end)) +
abs(count(1) - 1 - count(case status when 'approved' then 1 end)) = 0

关于sql - 如何在Oracle中查找相关值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17948935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com