SQL:如何根据某个字段对已查询的数据进行去重处理

方案一

首先,取出来每行数据的最大时间(即最新时间),然后让原表数据和最大时间做右连接,得到的就是最新的数据。

SELECT
  a0.*
FROM
  t_score a0
  RIGHT JOIN (
    SELECT
      max(ts) tsMax,
      id
    FROM
      t_score
    GROUP BY
      id
  ) b0 ON a0.ts = b0.tsMax
  AND a0.id = b0.id

 方案二

方案二为方案一的变种,使用了exists 关键字来获取时间上最新的数据

SELECT
  a0.*
FROM
  t_score a0
WHERE
  EXISTS (
    SELECT
      *
    FROM
      (
        SELECT
          max(ts) tsMax,
          id
        FROM
          t_score
        GROUP BY
          id
      ) b0
    WHERE
      b0.tsMax = a0.ts
      AND b0.id = a0.id
  )

实战代码

  把原来的整个表全部替换上去看效果用的(即:将你的查询sql替换“t_score”)

select *
  from (SELECT
  a0.*
FROM
  (select rw.id, rw.rwmc, lcrz.hjmc, lc.lcslmc, lcrz.sjclsj
          from WF_LCRZ lcrz, wf_lcsl lc, WF_RWSL rw
         where lcrz.ZXRBH = '121409'
           and lcrz.SJCLSJ >= '20220918000000'
           and lcrz.LCSLBH = lc.ID
           and lcrz.RWSLBH = rw.ID
           and rw.lcslbh = lc.id
           and rw.CLJG = '2'
           and lc.lczt <= 2
           and not exists (select id
                  from XM_ZBXM
                 where XM_ZBXM.ID = lc.ywbh
                   and lc.zlcbz = 0)
           and not exists (select id from wf_rwfpsl where wf_rwfpsl.id=rw.rwfpbh and wf_rwfpsl.type=6) --不展示阅知环节
          and  exists (select id from wf_hjsl where wf_hjsl.lcslbh=lc.id and wf_hjsl.BZ = 0)  --标志在用
                   and lcrz.hjmc is not null order by decode(lcrz.sjclsj, null, 0, lcrz.sjclsj) desc) a0
WHERE
  EXISTS (
    SELECT
      *
    FROM
      (
        SELECT
          max(sjclsj) tsMax,
          id
        FROM
          (select rw.id, rw.rwmc, lcrz.hjmc, lc.lcslmc, lcrz.sjclsj
          from WF_LCRZ lcrz, wf_lcsl lc, WF_RWSL rw
         where lcrz.ZXRBH = '121409'
           and lcrz.SJCLSJ >= '20220918000000'
           and lcrz.LCSLBH = lc.ID
           and lcrz.RWSLBH = rw.ID
           and rw.lcslbh = lc.id
           and rw.CLJG = '2'
           and lc.lczt <= 2
           and not exists (select id
                  from XM_ZBXM
                 where XM_ZBXM.ID = lc.ywbh
                   and lc.zlcbz = 0)
           and not exists (select id from wf_rwfpsl where wf_rwfpsl.id=rw.rwfpbh and wf_rwfpsl.type=6) --不展示阅知环节
          and  exists (select id from wf_hjsl where wf_hjsl.lcslbh=lc.id and wf_hjsl.BZ = 0)  --标志在用
                   and lcrz.hjmc is not null order by decode(lcrz.sjclsj, null, 0, lcrz.sjclsj) desc)
        GROUP BY
          id
      ) b0
    WHERE
      b0.tsMax = a0.sjclsj
      AND b0.id = a0.id
  ))
 where rownum <= 4

更改:Exists内容

select *
  from (SELECT
  a0.*
FROM
  (select rw.id, rw.rwmc, lcrz.hjmc, lc.lcslmc, lcrz.sjclsj
          from WF_LCRZ lcrz, wf_lcsl lc, WF_RWSL rw
         where lcrz.ZXRBH = '121409'
           and lcrz.SJCLSJ >= '20220918000000'
           and lcrz.LCSLBH = lc.ID
           and lcrz.RWSLBH = rw.ID
           and rw.lcslbh = lc.id
           and rw.CLJG = '2'
           and lc.lczt <= 2
           and not exists (select id
                  from XM_ZBXM
                 where XM_ZBXM.ID = lc.ywbh
                   and lc.zlcbz = 0)
           and not exists (select id from wf_rwfpsl where wf_rwfpsl.id=rw.rwfpbh and wf_rwfpsl.type=6) --不展示阅知环节
          and  exists (select id from wf_hjsl where wf_hjsl.lcslbh=lc.id and wf_hjsl.BZ = 0)  --标志在用
                   and lcrz.hjmc is not null order by decode(lcrz.sjclsj, null, 0, lcrz.sjclsj) desc) a0
WHERE
  EXISTS (
    SELECT
      *
    FROM
      (SELECT
          max(lcrz.sjclsj) tsMax,
          lcrz.RWSLBH rwslbh
        FROM
          WF_LCRZ lcrz
        GROUP BY
          lcrz.RWSLBH
      ) b0
    WHERE
      b0.tsMax = a0.sjclsj
      AND b0.rwslbh = a0.id
  ))
 where rownum <= 4

优化合并到原查询语句中:最后一个exists

select *
  from (select rw.id, rw.rwmc, lcrz.hjmc, lc.lcslmc, lcrz.sjclsj
          from WF_LCRZ lcrz, wf_lcsl lc, WF_RWSL rw
         where lcrz.ZXRBH = '121409'
           and lcrz.SJCLSJ >= '20220918000000'
           and lcrz.LCSLBH = lc.ID
           and lcrz.RWSLBH = rw.ID
           and rw.lcslbh = lc.id
           and rw.CLJG = '2'
           and lc.lczt <= 2
           and not exists (select id
                  from XM_ZBXM
                 where XM_ZBXM.ID = lc.ywbh
                   and lc.zlcbz = 0)
          and not exists (select id from wf_rwfpsl where wf_rwfpsl.id=rw.rwfpbh and wf_rwfpsl.type=6) --不展示阅知环节
          and  exists (select id from wf_hjsl where wf_hjsl.lcslbh=lc.id and wf_hjsl.BZ = 0)  --标志在用
          and exists (select * from (select max(SJCLSJ) tsMax,RWSLBH from WF_LCRZ group by RWSLBH) te  where lcrz.SJCLSJ = te.tsMax and te.RWSLBH=lcrz.RWSLBH)
                   and lcrz.hjmc is not null order by decode(lcrz.sjclsj, null, 0, lcrz.sjclsj) desc)
 where rownum <= 4

 方案三

使用 row_number() over (parttion by 分组列 order by 排序列) 方式

SELECT
	* 
FROM
	( SELECT *, row_number() over ( PARTITION BY id ORDER BY ts DESC ) num FROM t_score ) a0 
WHERE
	a0.num = 1

猜你喜欢

转载自blog.csdn.net/weixin_63610637/article/details/129988269