【问题标题】:Distinct in concatenated columns from a table in ms sqlms sql中表的连接列不同
【发布时间】:2026-02-16 23:15:02
【问题描述】:

我在我的数据库中运行此查询,结果并不明显。在 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..。如果可能,添加示例数据和预期输出,这将帮助您获得正确的答案。

标签: mysql sql-server concatenation distinct distinct-values


【解决方案1】:

只需在 select 子句中使用 Distinct。

【讨论】:

    【解决方案2】:

    试试这个:

    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
        )
    

    【讨论】: