我有一个包含3个字段RecordID,PhoneNumber,Disposition的表。我正在使用SQL服务器。
案例1: -
RecordID PhoneNumber Disposition
2345 9212345667 PNP
2345 9212345667 PNP
2345 9212345667 PNP
4563 8234589012 PNP
这里我应该显示2行的单个记录。
Result : 2345 9212345667 PNP
4563 8234589012 PNP
只能根据RecordID,PhoneNumber和Disposition duplicates显示一条记录。
案例2: -
RecordID PhoneNumber Disposition
2345 9212345667 PNP
2345 9212345667 CBK
4563 8234589012 PNP
Result : 4563 8234589012 PNP
这里RecordID和PhoneNumber相同,但前两个记录的处理方式不同。因此不应显示记录。第三条记录与RecordID和PhoneNumber的重复值无关。所以它应该显示。
谢谢请帮助。
答案 0 :(得分:2)
要获取每一行并省略重复项,请使用distinct
select distinct RecordID, PhoneNumber, Disposition from mytable
要获取重复项不存在的行,请使用group by / having count(*) = 1
select RecordID, PhoneNumber, Disposition from mytable
group by RecordID, PhoneNumber, Disposition
having count(*) = 1
答案 1 :(得分:1)
Sql server有Top(1) with ties ... order by
加row_number()
Select Top(1) with ties RecordID, PhoneNumber, Disposition
from table
-- case 1
order by row_number() over (partition by RecordID, PhoneNumber, Disposition order by recordID)
-- case 2
--order by row_number() over (partition by RecordID, PhoneNumber order by recordID)
;