在 ms sql 中与 table 的串联列不同

Distinct in concatenated columns from a table in ms sql

我 运行 我的数据库中的这个查询结果不明确。在 select 子句中使用串联时,是否有任何解决方案可以获得不同的结果。

select
    case 
        when c.SubtypeId_FK is null then c.TypeDescription 
        else c.TypeDescription + ' In ' + cs.Subtype 
    end as Experties
from 
    CaseTLS c, 
    CaseLawyer cl , 
    Lawyer l , 
    CaseSubtype cs
where
    c.CaseId = cl.CaseID  
    and cl.ComputerCode = l.ComputerCode 
    and l.ComputerCode = @p1 
    and (
        c.SubtypeId_FK = cs.SubtypeId or c.SubtypeId_FK is null
    )

试试这个:

select
     DISTINCT case 
        when c.SubtypeId_FK is null then c.TypeDescription 
        else c.TypeDescription + ' In ' + cs.Subtype 
    end as Experties
from 
    CaseTLS c, 
    CaseLawyer cl , 
    Lawyer l , 
    CaseSubtype cs
where
    c.CaseId = cl.CaseID  
    and cl.ComputerCode = l.ComputerCode 
    and l.ComputerCode = @p1 
    and (
        c.SubtypeId_FK = cs.SubtypeId or c.SubtypeId_FK is null
    )

只需在 select 子句中使用 Distinct。