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

如何筛选SQL表中指定列值出现多次对应的所有数据行

解决方案

你当前使用dense_rank()是为同Pcode下的行按日期排序,无法直接统计Pcode的出现次数,调整为使用COUNT()窗口函数即可实现需求:

方案1:基于窗口函数实现(推荐,性能更好,仅需扫描一次表)

select 
    pcode,
    LogId,
    extdate
from
    (select 
         pcode,
         L.logid as LogId,
         extdate,
         -- 统计每个Pcode对应的总行数
         count(*) over (partition by pcode) as p_total
     from 
         kip_project_master P
     inner join 
         kip_report_extraction_log L on L.LogId = P.LogId) tbl
where p_total > 1
-- 可选:如果需要和预期输出一致按Pcode、LogId排序可添加下行
order by pcode, LogId

方案2:子查询匹配实现(兼容旧版本不支持窗口函数的SQL引擎)

select 
    P.pcode,
    L.logid as LogId,
    L.extdate
from 
    kip_project_master P
inner join 
    kip_report_extraction_log L on L.LogId = P.LogId
where P.pcode in (
    -- 先筛选出出现次数大于1的Pcode列表
    select pcode 
    from kip_project_master
    group by pcode
    having count(*) > 1
)
order by P.pcode, L.logid

如果还需要保留同Pcode下的日期排序序号,保留原来的dense_rank()计算逻辑,新增count窗口函数的过滤条件即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 05:24:03