如何消除重复?

时间:2016-10-17 12:46:40

标签: sql sql-server sql-server-2008

select C.Id as candidateId,C.Name, C.Phone, Status.ResultStatusText , Status.TimeStamp,  Status.notes ,
            (Select count(*) from CandidateCallHistory  where CandiateId =  candidateId) AS numbCalls,
            (SELECT SUBSTRING((SELECT ',' + Name
                FROM Jobs  
                WHERE Id in (select value from fn_Split(c.JobIds,','))
                FOR XML PATH('')),2,200000)) AS jobsList
    from  Candidate2 C
    outer APPLY  (select top 1 CH.CandiateId, CH.ResultStatusText , CH.TimeStamp , CH.notes  
                    from CandidateCallHistory CH    
                    where CH.CandiateId = C.Id          
                    order by TimeStamp desc) as Status 
    where Status.ResultStatusText <> 'completed' and Status.ResultStatusText <> 'canceled' and c.isactive = 1

我在CandidateCallHistory表中有多条记录,似乎这导致外部申请的问题(我可能是错的),因为它应该只获取表中的最新记录,因为它选择了前1。 / p>

1 个答案:

答案 0 :(得分:-2)

在选择后尝试在第一行添加distinct:

select distinct [...]