选择Oracle中的重复记录

-3 sql oracle

我在Oracle下面有一张表

表格1

State | Product |other fields
CA    | P1      | xxxx
OR    | P1      | xxxx
OR    | P1      | xxxx
OR    | P1      | xxxx
WA    | P1      | xxxx
VA    | P2      | xxxx
Run Code Online (Sandbox Code Playgroud)

只有在状态发生多次时才应选择我的输出.

State | Product |other fields
OR    | P1      | xxxx
Run Code Online (Sandbox Code Playgroud)

MT0*_*MT0 5

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

SELECT DISTINCT
       State,
       Product,
       Other_Fields
FROM   (
  SELECT t.*,
         COUNT(1) OVER ( PARTITION BY State ) AS cnt
  FROM   Table1 t
)
WHERE  cnt > 1;
Run Code Online (Sandbox Code Playgroud)

如果要考虑重复行(考虑所有字段),则:

SELECT *
FROM   Table1
GROUP BY State, Product, Other_Fields
HAVING COUNT(1) > 1;
Run Code Online (Sandbox Code Playgroud)