You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从SQL CTE表中查询ktokk列的不重复值

修改方案

你可以通过两种方式实现需求:

方法1:使用DISTINCT关键字(推荐,仅去重场景更直观)

直接修改外层查询,只查ktokk列并加去重关键字即可。另外注意:多数数据库会忽略CTE内部未搭配TOP/LIMIT的ORDER BY规则,建议把排序逻辑移到外层保证结果顺序符合预期:

with cte as 
(
    select 
        t.ktokk, t.txt30,
        case 
            when t.spras = 'P' then '1'
            when t.spras = 'E' then '2'
            else '3'
        end as ord_ktokk 
    from 
        t077y t 
)
select distinct ktokk 
from cte
order by ord_ktokk;

方法2:使用GROUP BY子句(适合需要同步做聚合计算的场景)

如果后续还要统计每个ktokk对应的其他字段指标,可以用分组逻辑实现去重:

with cte as 
(
    select 
        t.ktokk, t.txt30,
        case 
            when t.spras = 'P' then '1'
            when t.spras = 'E' then '2'
            else '3'
        end as ord_ktokk 
    from 
        t077y t 
)
select ktokk 
from cte
group by ktokk
-- 分组后排序需要用聚合函数包裹排序字段
order by min(ord_ktokk);

内容的提问来源于stack exchange,提问作者saraz0r

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 12:45:03