3

我有一个TABLEA数据如下表

field1 field2 field3.......field16
123    10-JAN-12 0.8.......ABC
123    10-JAN-12 0.8.......ABC
.
.
.
123    10-JAN-12 0.7.......ABC
245    11-JAN-12 0.3.......CDE
245    11-JAN-12 0.3.......CDE
245    11-JAN-12 0.3.......XYZ
...
<unique rows>

当我做一个

select field1, field2, ...field16 
  from TABLEA

我获得了 M 条记录,当我做一个

select distinct field1, field2...field16 
  from TABLEA

我获得了M-x记录,其中M是数百万,并且x是一个小得多的#。

我正在尝试编写 SQL 来获取 x 记录(最终,只需获取计数)。我已经尝试过所有 Set 运算符关键字,例如

select field1...field16 
 from TABLEA 
 EXCEPT 
 select distinct field1..field16 
   from TABLEA  

或使用UNION ALL代替 EXCEPT。但是它们都没有 return x,而是都返回 0 行。

4

3 回答 3

3

您可以选择不同的行

 SELECT field1, ... , field16
   FROM tablea
  GROUP BY field1, ... , field16
 HAVING count(*) > 1

编辑:另一种方法是使用分析函数,按所有列进行ROW_NUMBER()分区。field给定字段集的第一行(即不同的)具有ROW_NUMBER = 1,第二行 = 2,第三行 = 3 等。因此您可以选择x-rows with WHERE ROW_NUMBER > 1

CREATE TABLE tablea (
    field1 NUMBER, field2 DATE,  field3 NUMBER, field16 VARCHAR2(10)
);

INSERT INTO tablea VALUES (123, DATE '2012-01-10', 0.8, 'ABC');
INSERT INTO tablea VALUES (123, DATE '2012-01-10', 0.8, 'ABC');
INSERT INTO tablea VALUES (123, DATE '2012-01-10', 0.7, 'ABC');
INSERT INTO tablea VALUES (245, DATE '2012-01-11', 0.3, 'CDE');
INSERT INTO tablea VALUES (245, DATE '2012-01-11', 0.3, 'CDE');
INSERT INTO tablea VALUES (245, DATE '2012-01-11', 0.3, 'XYZ');

要选择重复的行x

SELECT *
  FROM (
        SELECT field1, field2, field3, field16,
               ROWID AS rid,
               ROW_NUMBER() OVER (PARTITION BY 
               field1, field2, field3, field16 ORDER BY ROWID) as rn
          FROM tablea
        )
  WHERE rn > 1;

 123 10.01.2012 0.8 ABC AAAJ6mAAEAAAAExAAB 2
 245 11.01.2012 0.3 CDE AAAJ6mAAEAAAAExAAE 2
于 2013-01-08T17:32:59.757 回答
1

您将通过上面发布的自己的“除外”查询获得所需的内容。但是您必须在您的 except 中包含“ALL”关键字,因为“Except Distinct”是默认设置。所以我刚刚在您的查询本身中添加了下面的 ALL 关键字:

从 TABLEA 中选择 field1...field16 EXCEPT ALL 从 TABLEA 中选择不同的 field1..field16

如果您想要 Mx 的记录计数,则使上述查询成为另一个查询的 FROM 子句中的子查询,并在该外部查询中计数,您将获得如下所示的计数:

Select count(*) From ( select field1...field16 from TABLEA EXCEPT ALL select distinct field1..field16 from TABLEA
) B

猜猜这就是你要找的。

祝你好运

于 2014-01-25T13:57:30.860 回答
0

如果您的列选择相同,您将不会获得不在您不同的行结果的计数。Distinct 显示了所有结果的“DISTINCT”可能性,因此执行 union all 只会重复它,而 except 永远不会找到任何东西,因为你限制了你的行。你到底想做什么?尝试计算差异发生在哪里?你从沃尔夫冈那里得到的答案已经做到了。

declare @Table Table ( personID int identity, person varchar(8));

insert into @Table values ('Brett'),('Brett'),('Brett'),('John'),('John'),('Peter');


-- gives me all results
select person
from @Table

-- gives me distinct results (no repeats)
Select distinct person
from @Table


-- gives me nothing as nothing exists that is distinct that is not in total
select person
from @Table 
except 
select distinct person
from @Table

-- shows me counts of rows repeated by pivoting on one column and counting resultant rows from that.  Having clause adds predicate specific logic to hunt for.
-- in this case duplicates or rows greater than one
Select person, count(*)
from @Table 
group by person
having count(*) > 1 

编辑如果这就是您的意思,您可以获得与总数不同的差异:

 with dupes as 
    (
    Select count(*) as cnts, sum(count(*)) over() as TotalDupes
    from @Table 
    group by person 
    having count(*) > 1 -- dupes are defined by rows repeating 
    ) 
, uniques as 
    (
    Select count(*) as cnts, sum(count(*)) over() as TotalUniques
    from @Table 
    group by person 
    having count(*) = 1  -- non dupes are rows of only a single resulting row
    )
select distinct TotalDupes - TotalUniques as DifferenceFromRepeatsToUnqiues
from Dupes, Uniques
于 2013-01-08T19:02:07.223 回答