Sql 选择Oracle中的重复记录

Sql 选择Oracle中的重复记录,sql,oracle,Sql,Oracle,我在甲骨文中有一个表格 表1 State | Product |other fields CA | P1 | xxxx OR | P1 | xxxx OR | P1 | xxxx OR | P1 | xxxx WA | P1 | xxxx VA | P2 | xxxx 仅当状态出现多次时,我的输出才应为select State | Product |other fields OR | P1

我在甲骨文中有一个表格

表1

State | Product |other fields
CA    | P1      | xxxx
OR    | P1      | xxxx
OR    | P1      | xxxx
OR    | P1      | xxxx
WA    | P1      | xxxx
VA    | P2      | xxxx
仅当状态出现多次时,我的输出才应为select

State | Product |other fields
OR    | P1      | xxxx

如果您只想计算重复状态,则:

SELECT DISTINCT
       State,
       Product,
       Other_Fields
FROM   (
  SELECT t.*,
         COUNT(1) OVER ( PARTITION BY State ) AS cnt
  FROM   Table1 t
)
WHERE  cnt > 1;
如果要考虑重复行(考虑所有字段),则:


您可以使用行数分析函数

比如说,

SQL> WITH sample_data AS(
  2  SELECT 'CA' State, 'P1' product FROM dual UNION ALL
  3  SELECT 'OR', 'P1' product from dual union all
  4  SELECT 'OR', 'P1' product FROM dual UNION ALL
  5  SELECT 'OR', 'P1' product FROM dual UNION ALL
  6  SELECT 'WA', 'P1' product FROM dual UNION ALL
  7  SELECT 'VA', 'P2' product from dual
  8  )
  9  -- end of sample_data mimicking real table
 10  SELECT distinct state,
 11    product
 12  FROM
 13    (SELECT state,
 14      product,
 15      row_number() OVER(PARTITION BY state ORDER BY product) rn
 16    FROM sample_data
 17    )
 18  WHERE rn >1;

ST PR
-- --
OR P1

SQL>

您尝试过任何东西吗?例如select语句?
select state, product, column_3, column_4
from (
  select state, product, column_3, column_4, 
         count(*) over (partition by state) as cnt
  from the_table
) t
where cnt > 1;
SQL> WITH sample_data AS(
  2  SELECT 'CA' State, 'P1' product FROM dual UNION ALL
  3  SELECT 'OR', 'P1' product from dual union all
  4  SELECT 'OR', 'P1' product FROM dual UNION ALL
  5  SELECT 'OR', 'P1' product FROM dual UNION ALL
  6  SELECT 'WA', 'P1' product FROM dual UNION ALL
  7  SELECT 'VA', 'P2' product from dual
  8  )
  9  -- end of sample_data mimicking real table
 10  SELECT distinct state,
 11    product
 12  FROM
 13    (SELECT state,
 14      product,
 15      row_number() OVER(PARTITION BY state ORDER BY product) rn
 16    FROM sample_data
 17    )
 18  WHERE rn >1;

ST PR
-- --
OR P1

SQL>