gpt4 book ai didi

mysql - 选择不同但具有列的值

转载 作者:行者123 更新时间:2023-11-29 06:25:40 25 4
gpt4 key购买 nike

这就是我想要的。

假设我们有这张表

Name  Email          Department             Branch
------------------------------------------------------
John john@test.ca Information Technology SAP
John john@test.ca Not Available Not Available
Bret brett@test.ca Emergency Response 911
Bret brett@test.ca Not Available Not Available
diana diana@test.ca Not Available Not Available

现在我基本上想要的是得到以下结果:

如果约翰有“信息技术”和“不可用”,则“信息技术”将成为优先级。基本上,如果约翰不属于任何其他部门,它只会显示“不可用”行。所以结果表将是这样的:

Name  Email         Department             Branch
-----------------------------------------------------
John john@test.ca Information Technology SAP
Bret brett@test.ca Emergency Response 911
Diana diana@test.ca Not Available Not Available

谢谢!

最佳答案

对于此示例数据,您需要一个 CASE 表达式来检查 'Not available' 并将其视为 NULL,以便您可以聚合 (与 MINMAX):

select name, email,
coalesce(
max(case when department = 'Not Available' then null else department end),
'Not Available'
) department,
coalesce(
max(case when branch = 'Not Available' then null else branch end),
'Not Available'
) branch
from tablename
group by name, email

请参阅demo .
结果:

| name  | email         | department             | branch        |
| ----- | ------------- | ---------------------- | ------------- |
| Bret | brett@test.ca | Emergency Response | 911 |
| diana | diana@test.ca | Not Available | Not Available |
| John | john@test.ca | Information Technology | SAP |

关于mysql - 选择不同但具有列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59632202/

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