是否可以在同一个表格中显示重复的行? 我试图在sql开发人员上进行此查询,但它仍在搜索一段时间而没有给我结果甚至错误消息,这是我到目前为止尝试的最后一个查询:
SELECT foo.COL_1, foo.COL_2, foo.COL_3, foo.COL_4, foo.COL_5, foo.COL_6, foo.COL_7
FROM TABLE_NAME foo
WHERE EXISTS (SELECT bar.COL_1, bar.COL_2, bar.COL_3, bar.COL_4, bar.COL_5, bar.COL_6, bar.COL_7
FROM TABLE_NAME bar
WHERE foo.COL_1 = bar.COL_1
AND foo.COL_2 = bar.COL_2
AND foo.COL_3 = bar.COL_3
AND foo.COL_4 = bar.COL_4
AND foo.COL_5 = bar.COL_5
AND foo.COL_6 = bar.COL_6
AND foo.COL_7 = bar.COL_7
GROUP BY bar.COL_1, bar.COL_2, bar.COL_3, bar.COL_4, bar.COL_5, bar.COL_6, bar.COL_7)
GROUP BY foo.COL_1, foo.COL_2, foo.COL_3, foo.COL_4, foo.COL_5, foo.COL_6, foo.COL_7
ORDER BY foo.COL_7
;
注意:
我不需要看到一个带有计数的行,告诉我它存在2次或更多次,否则我会放置主键以防止重复。我必须在报告上打印这些数据。
我只喜欢发布的数据。
谢谢
答案 0 :(得分:0)
我们可以在此处使用COUNT
作为分析函数:
SELECT COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7
FROM
(
SELECT COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7,
COUNT(*) OVER (PARTITION BY COL_1, COL_2, COL_3, COL_4, COL_5,
COL_6, COL_7) cnt
FROM TABLE_NAME
) t
WHERE cnt > 1
ORDER BY COL_7;
这将显示具有重复项的所有记录。如果您只希望每个重复组中只有一条记录,那么我们可以修改上述查询。