gpt4 book ai didi

SQL(PLSQL),如何选择不同但计数> 1

转载 作者:搜寻专家 更新时间:2023-10-30 20:23:08 24 4
gpt4 key购买 nike

我有一个包含 ID_STUFF、STUFF_NAME、E_MAIL 列的表 INVOICE。我需要找到具有相同 ID_STUFF 但不同 STUFF_NAME 或 E_MAIL 的行。

select distinct g.id_stuff, g.staff_name, g.e_mail from invoice g

显示这个: enter image description here

但我不需要具有相同 ID_STUFF 相同值的行。

最佳答案

大概,您需要原始行。如果是这样,我建议使用 exists 两次:

select i.*
from invoice i
where exists (select 1
from invoice i2
where i2.id_stuff = i.id_stuff and
i2.staff_name <> i.staff_name
) or
exists (select 1
from invoice i2
where i2.id_stuff = i.id_stuff and
i2.e_mail <> i.e_mail
) ;

查询可以利用 invoice(id_stuff, e_mail)invoice(id_stuff, staff_name) 上的索引——这将是一个很大的性能胜利表。

如果您只想要id_stuff,那么group by 是一个很好的解决方案。您可以使用 listagg() 获取姓名和电子邮件列表:

select i.id_stuff,
listagg(e_mail, ',') within group (order by e_mail),
listagg(staff_name, ',') within group (order by staff_name)
from invoice i
group by i.id_stuff
having min(e_mail) <> max(e_email) or
min(staff_name) <> max(staff_name);

关于SQL(PLSQL),如何选择不同但计数> 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53572320/

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