gpt4 book ai didi

sql - 查询根据特定条件过滤记录

转载 作者:行者123 更新时间:2023-12-04 21:42:41 24 4
gpt4 key购买 nike

There is a table a_status_check with the following data:

enter image description here

要求是:如果状态LC和BE都存在,那么只考虑LC。否则,请考虑 BE。忽略该 ID 的其他代码。

所以,结果应该是这样的:

enter image description here

我尝试了 DECODE 和 CASE 函数,但没有成功。有人可以帮忙吗。

最佳答案

使用分析函数:

select distinct
id,
first_value (status) over (partition by id order by status desc) status,
first_value (amt ) over (partition by id order by status desc) amt
from
tq84_a_status_check
where
status in ('LC', 'BE')
order by
id;

测试数据:

create table tq84_a_status_check (
id number,
status varchar2(10),
amt number
);

select distinct
id,
first_value (status) over (partition by id order by status desc) status,
first_value (amt ) over (partition by id order by status desc) amt
from
tq84_a_status_check
where
status in ('LC', 'BE')
order by
id;

关于sql - 查询根据特定条件过滤记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30932071/

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