【问题标题】:Show duplicate rows(all columns of that row) where all columns are duplicate except one column显示重复的行(该行的所有列),其中所有列都是重复的,除了一列
【发布时间】:2018-03-24 00:39:30
【问题描述】:

在下表中,我需要选择重复记录,其中除特定周的客户类型和价格外,所有列都是重复的。

例如

Week Customer  Product  Customer Type   Price
1    Alex      Cycle    Consumer        100
1    Alex      Cycle    Reseller        101
2    John      Motor    Consumer        200
3    John      Motor    Consumer        200
3    John      Motor    Reseller        201

我正在使用以下查询,但此查询并未显示两种客户类型,它仅显示组合的消费者计数 (*)。

select Week, Customer, product, count(distinct Customer Type)
from table
group by Week, Customer, product
having count(distinct Customer Type) > 1

我想看到下面的结果,它显示了重复的值,而不仅仅是重复行的计数(*)。我试图查看在特定周内分配给产品的多个客户类型的客户,同时向我显示所有列。价格不同也没关系。

Week Customer  Product  Customer Type   Price
1    Alex      Cycle    Consumer        100
1    Alex      Cycle    Reseller        101
3    John      Motor    Consumer        200
3    John      Motor    Reseller        201

谢谢

沙基

【问题讨论】:

  • 您正在使用哪个RDBMS,请标记它。
  • 请添加一个标签,告诉我们您使用哪个数据库。 “SQL”不足以告诉使用建议的选项和语法。
  • 如果您的数据库支持,您似乎需要 row_number

标签: sql teradata


【解决方案1】:
 WITH CustomerDistribution_CTE (WeekC ,CustomerC,  ProductC)
    AS
    (
    select Week, Customer, product
    from Your_Table_Name group by Week, Customer, 
    product having count(distinct CustomerType) > 1
    )
    SELECT Y.*
    FROM CustomerDistribution_CTE C
    inner join  Your_Table_Name  Y on C.WeekC =Y.Week
    and  C.CustomerC =Y.Customer  and  C.productC =Y.product

注意:请将“Your_Table_Name”替换为准确的表名并尝试。

【讨论】:

  • 第一行的“(WeekC ,CustomerC, ProductC)”有什么作用?无论有没有它,查询似乎都会返回相同的结果。这是否只是澄清了您从 AS 子查询中提取的列?
【解决方案2】:

使用通用 SQL 实现此目的的一种方法是使用这样的“派生表”:

select x.*
from tablex x
inner join (
    select Week, Customer, Product 
    from tablex 
    group by Week, Customer, Product
    having count(*) > 1
    ) d on x.Week = d.Week and x.Customer = d.Customer and x.Product = d.Product

【讨论】:

    【解决方案3】:

    您可以通过使用DISTINCT 来做到这一点

    select DISTINCT Customer,Product,Customer_Type,Price from Your_Table_Name
    

    将寻找 DISTINCT 组合。
    注意:此查询 if 的 SQL Server

    【讨论】:

    • 你没有考虑周。此查询也将返回不应返回的非重复行。
    • 请查看预期结果,它可能有差异。一周。
    【解决方案4】:

    从您粘贴的预期结果来看,您似乎并不关心这一周。 如果你有一个ID(增量PK),它会像下面这样简单得多

    从 ID 不在的表中选择 * (按客户、产品、客户类型从表组中选择 max(ID),count(*) > 1)

    这是在 MySQL 上测试的。你有 ID 列吗? 如果您没有 ID 列,请尝试以下操作:

    按客户、产品、客户类型从设备组中选择最大(周)周、客户、产品、客户类型、最大(价格);

    我还没有验证这个。

    【讨论】:

      【解决方案5】:

      这将返回您预期的结果集:

      select *
      from table
      -- Teradata syntax to filter the result of an OLAP-function
      -- (similar to HAVING after GROUP BY)
      qualify
         count(*)
         over (partition by Week, Customer, product) > 1
      

      对于其他 DBMS,您需要嵌套查询:

      select *
      from
       (
          select ..., 
             count(*)
             over (partition by Week, Customer, product) as cnt
          from table
       ) as dt
      where cnt > 1
      

      编辑:

      重新阅读您上面的描述后,Select 可能不是您想要的,因为它还会返回具有单一类型的行。然后切换到:

      select *
      from table
      -- Teradata syntax to filter the result of an OLAP-function
      -- (similar to HAVING after GROUP BY)
      qualify -- at least two different types:
         min(Customer_Type) over (partition by Week, Customer, product)
      <> max(Customer_Type) over (partition by Week, Customer, product)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-27
        • 2020-12-29
        相关资源
        最近更新 更多