Oracle从重复数据中选择MAX

时间:2017-05-28 07:39:11

标签: sql oracle select duplicates

我需要选择一个带有'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 

1 个答案:

答案 0 :(得分:0)

尝试

SELECT doc_no, sub, COUNT (*)
    FROM (SELECT DISTINCT doc_no, sub FROM maindata)
GROUP BY doc_no, sub
ORDER BY doc_no;

不确定为什么你有子查询来获得密集排名的主题,如果你坚持使用子查询,你可以尝试

  SELECT doc_no, sub, COUNT (*)
    FROM (SELECT DISTINCT
                 doc_no,
                 (SELECT MAX (sub)
                            KEEP (DENSE_RANK FIRST ORDER BY VSIZE (sub) DESC)
                    FROM test_data md2
                   WHERE md.doc_no = md2.doc_no)
                    sub
            FROM test_data md)
GROUP BY doc_no, sub
ORDER BY doc_no;

Demo