【发布时间】:2017-10-28 17:22:47
【问题描述】:
我只需要选择一个带有“DOC_NO”的记录,但结果是重复的。
SELECT DOC_NO
, (SELECT MAX(SUBJECT) KEEP(DENSE_RANK FIRST ORDER BY vsize(SUBJECT) DESC)
FROM MainData MD2
WHERE MD.DOC_NO = MD2.DOC_NO) AS SUB
, count(*)
FROM MainData MD
WHERE 1=1
GROUP BY DOC_NO, SUBJECT
ORDER BY count(*) desc;
→ 我预计结果计数将全部为 1。但结果中,有些是 2、3..
原始数据如下
(...) DOC_NO SUB
111a 'the example'
111a 'the example'
111a 'the example'
222a 'the example2'
222a 'the example2'
333a 'the example3'
333a 'the example3'
→ 两者 (DOC_NO, SUB) 都不是 PK。所以允许复制。
请告诉我如何在重复中选择一条记录。
添加和编辑>>
我预期的结果是
DOC_RNO SUB count(*)
111a 'the example' 1
222a 'the example2' 1
333a 'the example3' 1
结果是
DOC_RNO SUB count(*)
111a 'the example' 3
222a 'the example2' 2
333a 'the example3' 2
【问题讨论】:
-
给出一些带有必要列的示例数据行以及基于此的预期输出
-
没有你的样本数据,它给你这个输出,很难给你任何答案
-
感谢您的建议!我编辑了。
-
GROUP BY 中的 SUBJECT 列在哪里?你不是说 SUB 吗?
-
您是否只想为每个
docNo和sub组合硬编码1?如果是,那么只需使用select DOC_RNO, SUB, 1 as count1 from your table group by DOC_RNO, SUB
标签: sql oracle select duplicates