首页 > 解决方案 > 检查不同的列

问题描述

我有一个名为Person的表,其中包含三列Name、Designation、Address

如果地址重复,我需要找到 Name 和 Designation 的唯一组合,如果是,我需要标记为trueelse 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 ”。

标签: sqlsql-servertsql

解决方案


重复意味着不同地址的数量与总数不匹配。所以:

select name, designation,
       (case when count(distinct Address) < count(*) then 'true' else 'false') as is_repeated
from person
group by name, designation;

推荐阅读