Sql 显示重复行(该行的所有列),其中除一列外,所有列都是重复的

Sql 显示重复行(该行的所有列),其中除一列外,所有列都是重复的,sql,teradata,Sql,Teradata,在下表中,我需要选择重复记录,其中除客户类型和特定周的价格外,所有列都是重复的 例如 Week Customer Product Customer Type Price 1 Alex Cycle Consumer 100 1 Alex Cycle Reseller 101 2 John Motor Consumer 200 3 John Motor Consu

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

例如

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
谢谢


Shaki

实现这一点的一种方法是使用通用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
您可以使用DISTINCT like来实现这一点

将寻找不同的组合。
注意:如果从您粘贴的预期结果中查询SQL Server,则看起来您并不关心本周。 如果您有一个ID增量PK,它将更简单,如下所示

从ID不在的表中选择* 按客户、产品、计数大于1的客户类型从表组中选择maxID

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

按客户、产品、客户类型从设备组中选择maxweek week、Customer、Product、CustomerType、maxprice

我还没有证实这一点

 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

注意:请用确切的表名替换表名,然后重试

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

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)

请标记您正在使用的RDBMS。请添加一个标记,告诉我们您使用的数据库。SQL不足以告诉您应该使用什么选项和语法。看起来您需要行号,如果您的DB支持它,则您本周不考虑。此查询也将返回不应返回的非重复行。请查看预期结果,它可能有不同的星期。第一行的WeekC、CustomerC、ProductC的用途是什么?该查询似乎返回相同的结果,有或没有。这是否只是澄清了从AS子查询中提取的列?
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)