Oracle - 从GROUP BY表达式中选择完整行

时间:2014-04-10 13:36:15

标签: sql oracle group-by

我希望使用我的结果组选择完整行。这样我就可以分别检查行了。这就是我提出的,但它没有返回任何东西,我不知道为什么。

SELECT * FROM myTable t ,
  ( SELECT
        prop_1, prop_2 , prop_3  
    FROM
       ( SELECT COUNT(*) AS countx,  prop_1, prop_2, prop_3 FROM myTable
         GROUP BY  prop_1, prop_2, prop_3 )  
    WHERE countx>1 )  subselect
WHERE
        t.prop_1 = subselect.prop_1
    AND t.prop_2  = subselect.prop_2
    AND t.prop_3  = subselect.prop_3 ;

也许我应该尝试完全不同的方法,但请解释我为什么这不起作用

2 个答案:

答案 0 :(得分:1)

我需要你需要这样的东西:

select * from myTable where count(*) over (partition by prop_1, prop_2,prop_3) >1

或者如果你需要重复,也许这样更干净:

select * from myTable t1, myTable t2 where
  t1.prop_1 = t2.prop_1 and
  t1.prop_2 = t2.prop_2 and
  t1.prop_3 = t2.prop_3 and
  t1.id <t2.id

否则,您可以在最内部的一个中使用count(*)&gt; 1来合并内部2个查询,而不是第二个中的where。

答案 1 :(得分:1)

删除中间子查询。试试这个:

SELECT * FROM myTable t ,
       ( SELECT COUNT(*) AS countx,  prop_1, prop_2, prop_3 FROM myTable
         GROUP BY  prop_1, prop_2, prop_3 ) subselect
WHERE
        t.prop_1 = subselect.prop_1
    AND t.prop_2  = subselect.prop_2
    AND t.prop_3  = subselect.prop_3 ;