选择Oracle中的重复记录

时间:2016-02-11 10:24:17

标签: 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

我的输出应仅在状态多次出现时选择。

State | Product |other fields
OR    | P1      | xxxx

3 个答案:

答案 0 :(得分:5)

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

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

如果你想考虑重复的行(考虑所有字段),那么:

SELECT *
FROM   Table1
GROUP BY State, Product, Other_Fields
HAVING COUNT(1) > 1;

答案 1 :(得分:1)

soap_ssl_client_context(soap,SOAP_SSL_NO_AUTHENTICATION|SOAP_TLSv1,NULL,NULL,NULL,NULL,NULL)

答案 2 :(得分:1)

您可以使用 ROW_NUMBER 分析功能。

例如,

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>