gpt4 book ai didi

sql - 使用多个 case 语句进行分区

转载 作者:行者123 更新时间:2023-12-03 15:51:05 25 4
gpt4 key购买 nike

我正在尝试使用 partition by 子句对数据库中的记录进行重复数据删除 这是我为了进行重复数据删除而运行的查询。它对人口最多的记录进行排名,并保留排名最高的记录。

   WITH cteDupes AS 
(
--
-- Partition based on contact.owner and email
SELECT ROW_NUMBER() OVER(PARTITION BY contactowner, email
ORDER BY
-- ranking by populated field
case when otherstreet is not null then 1 else 0 end +
case when othercity is not null then 1 else 0 end
) AS RND, *
FROM scontact
where (contact_owner_name__c is not null and contact_owner_name__c<>'') and (email is not null and email<>'')
)
--Rank data and place it into a new table created
select * into contact_case1
from cteDupes
WHERE RND=1;

我想知道是否可以通过用例进行分区。例如,目前我正在按联系人所有者和电子邮件进行分区。当contactowner 为空时,我想改为按contactofficer 进行分区。
我可以创建这样的案例陈述吗,或者这是不可能的,因为排名会以某种方式改变。

最佳答案

您可以使用 case ,但我认为 coalesce()在这种情况下更简单:

SELECT ROW_NUMBER() OVER (PARTITION BY COALESCE(contactowner, contactofficer), email
. . .

如果您希望将同名的联系人和官员分开计算,那么您可以这样做:
SELECT ROW_NUMBER() OVER (PARTITION BY (CASE WHEN contactowner is NULL then 1 else 2 end),
contactowner,
(CASE WHEN contactowner is null THEN contactofficer END),
email
. . .

关于sql - 使用多个 case 语句进行分区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37757113/

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