【问题标题】:Fetching duplicate records based on a column value in ORACLEORACLE中根据列值获取重复记录
【发布时间】:2021-06-09 20:23:43
【问题描述】:

所以我有一个客户数据库,其中包含 Customer_Account_ID、Phone_No 和 Alt_Phone_No。现在 Alt_Phone_No 可以有一个数字值,也可以有空白。我想要一个数据格式如下的输出:

假设客户 ID 是 12345678 电话号码是 4021234444 和 4022221234 我们应该在电子表格上有两行:

Customer ID       Phone number  
12345678            4021234444
12345678            4022221234 

如果 Alt_Phone_No 中没有值,则不应有任何第二个条目,即在这种情况下只需要客户 ID 和电话号码。

我大致想到的如下:

SELECT Customer_Account_ID, Phone_No FROM Table_A a, 
  (SELECT Customer_Account_ID,  Phone_No FROM Table_A, Table_B
where Table_A.Cust_ID = Table_B.Cust_ID 
and condition = ' ' /*filter needed as per requirements*/
UNION ALL
SELECT Customer_Account_ID,  Alt_Phone_No FROM Table_A, Table_B
where Table_A.Cust_ID = Table_B.Cust_ID 
and condition = ' ' /*filter needed as per requirements*/ ) b
where a.Cust_ID = b.Cust_ID
and rownum < 1000 /*need rownum for testing sample data as the original data is too large*/
order by a.Cust_Id

我认为查询不正确。非常感谢所有的建议和意见。

【问题讨论】:

    标签: sql oracle10g


    【解决方案1】:

    你可以使用union all:

    select customerid, phonenumber
    from t
    union all
    select customerid, altphonenumber
    from t
    where altphonenumber is not null;
    

    【讨论】:

    • 我尝试了 Union All,但我认为这不起作用。即使客户有 2 个电话号码,结果集也只有其中的一组,即结果不是指定的格式。只返回一行,而应该有两行,每个电话号码一个。
    • @Aabhish 。 . .这将返回两个数字。但是,如果您希望将客户的行放在一起,则需要添加 order by customerid
    • 。 .请参阅问题中提到的查询。
    猜你喜欢
    • 1970-01-01
    • 2021-07-13
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多