gpt4 book ai didi

SQL - 如何制作排除条件

转载 作者:行者123 更新时间:2023-11-29 12:34:46 28 4
gpt4 key购买 nike

我有下表。我正在尝试获取满足我的特定条件的行。

表格如下:

account|transactiontypecode|
-------|-------------------|
1000058| 8|
1000067| 2|
1000067| 8|

查询输出将仅检索账户 1000058,因为它适用于交易类型代码 8。另一个账户也适用,但它还有另一个不适用的交易类型代码。

因此要求是获取满足特定交易代码的帐户,并排除帐户,即使它也可以具有所需的代码但也具有不需要的代码。

这是我对上述问题的猜测,但我认为其他人的眼睛可能会指引我走向更好的方向。

with cte1 as (
select
gp.account,
case
when gp.transactiontypecode in (2,8,17) then TRUE
else false
end as txcheck
from
gp.t2001 gp
group by
1, 2)

select
account,
txcheck
from
cte1
where
txcheck is true and txcheck is not false;

如果有人能帮助我实现上述要求,那就太好了!

最佳答案

如果您想要整行,只需使用 not exists:

select t.*
from gp.t2001 t
where t.transactiontypecode = 8 and
not exists (select 1
from gp.t2001 t2
where t2.account = t.account and
t2.transactiontypecode <> 8
);

如果您只想要帐户,或者聚合:

select t.account
from gp.t2001 t
group by t.account
having min(transactiontypecode) = max(transactiontypecode) and
min(transactiontypecode) = 8;

关于SQL - 如何制作排除条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56350126/

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