T-SQL仅根据一列x查找重复记录

时间:2017-08-09 23:10:19

标签: sql-server tsql

我的表格如下:

clm_id  process_id     note
N500    11            leg fracture
N500    12            leg fracture
N500    13            delivery
N500    14            headache
N501    5             surgery
N501    6             fever
N501    7             prostate
N501    8             prostate

这是理想的结果:

claim_id process_id    note
N500     11           leg fracture
N500     12           leg fracture
N501     7            prostate
N501     8            prostate

我该怎么做呢?

1 个答案:

答案 0 :(得分:1)

一个快速选项是使用sum() over ()

示例

;with cte as (
    Select *
          ,Cnt = sum(1) over (Partition By  clm_id,note)
     From  YourTable
)
Select clm_id
      ,process_id
      ,note
 From  cte 
 Where Cnt>1

<强>返回

clm_id  process_id  note
N500    11          leg fracture
N500    12          leg fracture
N501    7           prostate
N501    8           prostate