如何查看Oracle SQL中不同列中的重复项?

fzsnzjdm  于 2023-05-28  发布在  Oracle
关注(0)|答案(1)|浏览(127)

我试图找到重复,但同时能够在不同的列中看到它,但这个重复是在同一份报告中,我也按年验证了重复。
| 报告|ID头端|日期|持续时间|来源|尖端|碳纳米管|
| - -----|- -----|- -----|- -----|- -----|- -----|- -----|
| 一二三四|1111212| 22年12月31日|九个|S04| BS| 2|
| 三四五六|2233242| 20/10/22|八十|K34| LS| 2|
这是我在Oracle SQL Developer中执行的查询:

SELECT *
FROM   (
        SELECT ID_PPAL_ICI AS report,
               BLNC_ID AS id_tip,
               BLNC_FEC_CIERRE AS date,
               BLNC_DUR AS duration,
               ID_PPAL_FNT AS source,
               BLNC_TIP AS tip,
               COUNT(*) OVER ( PARTITION BY ID_PPAL_ICI, EXTRACT(YEAR FROM BLNC_FEC_CIERRE)) AS cnt
                 
FROM   BLNC_BALANCES
  
    INNER JOIN ID_PPAL
    ON BLNC_ID_PPAL_ICI = ID_PPAL_ICI
         
ORDER BY BLNC_FEC_CIERRE DESC
)
WHERE  cnt > 1;

select * from BLNC_BALANCES;

但我希望看到这样一张table:
| 报告|ID头端|日期|持续时间|来源|尖端|id_tip_1|日期_1|持续时间_1|源_1| tip_1|
| - -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|- -----|
| 一二三四|1111212| 22年12月31日|九个|S04| BS| 1122215| 24/03/22|一个|公司简介|BS|
| 三四五六|2233242| 20/10/22|八十|K34| LS| 4455385| 15/02/22|十一|H12| NS|
我已经尝试了左连接和HAVING COUNT,但我所有的都是错误的。有人帮忙吗?,拜托
注意:每行的重复项数量最多为一个。

tuwxkamq

tuwxkamq1#

您可以在不匹配的日期上进行自连接,同时保持重复项的分区不变,方法是匹配:

  • report”值
  • date”值的年份
WITH cte AS (
    SELECT ID_PPAL_ICI       AS report,
           BLNC_ID           AS id_tip,
           BLNC_FEC_CIERRE   AS date,
           BLNC_DUR          AS duration,
           ID_PPAL_FNT       AS source,
           BLNC_TIP          AS tip,
    FROM BLNC_BALANCES
    INNER JOIN ID_PPAL ON BLNC_ID_PPAL_ICI = ID_PPAL_ICI
)
SELECT t1.*,
       t2.id_tip   AS id_tip_1,
       t2.date     AS date_1,
       t2.duration AS duration_1,
       t2.source   AS source_1,
       t2.tip      AS tip_1
FROM       cte t1
INNER JOIN cte t2
        ON t1.report = t2.report 
       AND EXTRACT(YEAR FROM t1.date) = EXTRACT(YEAR FROM t2.date)
       AND NOT t1.date = t2.date

相关问题