sql - 检查不同的列
问题描述
我有一个名为Person的表,其中包含三列Name、Designation、Address。
如果地址重复,我需要找到 Name 和 Designation 的唯一组合,如果是,我需要标记为true
else false
。
样本:
Name Designation Address
Alex Manager Houston
Alex Manager Houston
Bailey Worker Boston
Bailey Worker New York
O/P:
Name Designation Repeated
Alex Manager true
Bailey Worker false
我知道直到我可以对列进行分组,我试过这样
select Name, Designation, Address
from Person
group by Name, Designation, Address
编辑:如果 Alex 有地址“ Phoenix ”,那么结果也将是相同的,即重复列是“ true ”,因为 Alex 已经在两行中重复了“ Houston ”。
解决方案
重复意味着不同地址的数量与总数不匹配。所以:
select name, designation,
(case when count(distinct Address) < count(*) then 'true' else 'false') as is_repeated
from person
group by name, designation;
推荐阅读
- python - 尝试使用 sounddevice 模块播放音频数据,但我很卡住
- c++ - 虚幻引擎项目中的 ReSharper 出现错误错误
- mongodb - MongoDB Mongoose 使用 Map 查找
- css - 如何为 Element => ElementClass => Child Class 编写 SCSS
- php - 使用PHP的fputcsv时如何写引号(“”)
- python - 试图做一个检查回文函数
- python - 是否可以使用没有插值的样条线(或任何其他 Python 库)使 matplotlib-plot 连接稀疏点?
- json - StatusCode:404,ReasonPhrase:'未找到',版本:1.1,内容:System.Net.Http.HttpConnectionResponseContent,
- aem - 体验片段的实时副本 - AEM 6.5
- sql-server - sqlservr.exe 总是低优先级