【问题标题】:How to obtain count of record differences in the same table, where there are distinct and nearly-distinct records如何获取同一张表中的记录差异计数,其中有不同的和几乎不同的记录
【发布时间】:2012-12-22 15:26:28
【问题描述】:

我有一张表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。但是它们都没有返回x,而是都返回0行。

【问题讨论】:

    标签: sql oracle distinct netezza


    【解决方案1】:

    您可以选择不区分的行

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

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

    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
    

    【讨论】:

    • 谢谢沃尔夫冈。该查询不会返回所需的结果。事实上,它只返回一行。不幸的是,基础表已更新,我的 x 已更改。
    【解决方案2】:

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

    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
    

    【讨论】:

    • 感谢两位抽出宝贵时间提供帮助。我要做的是找到以下两个查询之间的区别(1)从 TABLEA 中选择 field1、field2、...field16 和(2)从 TABLEA 中选择不同的 field1、field2、...field16 当计数为单独取并减去有一个小的差异(x)。我正在尝试查找提供 x 条记录的查询。
    • 我的“编辑”可能会回答这个问题。虽然我不确定这些数据会有什么用处。
    【解决方案3】:

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

    选择字段 1...字段 16 来自 TABLEA 除了所有 选择不同的 field1..field16 来自 TABLEA

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

    选择计数(*) 从 ( 选择字段 1...字段 16 来自 TABLEA 除了所有 选择不同的 field1..field16 来自 TABLEA
    ) 乙

    猜猜这就是你要找的东西。

    祝你好运

    【讨论】:

      猜你喜欢
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多