簡體   English   中英

oracle sql中多列表中一列的唯一值

[英]Unique values of one column in a multiple column table in oracle sql

我想用另一個表中的列創建一個新表。 只應選擇那些行,其中x列具有唯一值。 列 x 應包含列a的修剪值。

這是我的代碼:

create table nodupli as 
    select distinct(regexp_replace(a,'[[:space:]]|[[:punct:]]','')) as x,
        B, 
        C, 
        D
    from table1
order by x;

如何僅在 x 列中包含具有唯一值的行?

您可以將該查詢與另一個只返回唯一x值的查詢連接起來,例如

select  x
from    table1
group by x
having count(*) = 1

結果查詢將是

create table nodupli as 
select  regexp_replace(t1.a,'[[:space:]]|[[:punct:]]','') as x,
        t1.B, 
        t1.C, 
        t1.D
from    table1 t1
join    (
            select  regexp_replace(a,'[[:space:]]|[[:punct:]]','') as x
            from    table1
            group by regexp_replace(a,'[[:space:]]|[[:punct:]]','')
            having count(*) = 1
        ) t2
on      regexp_replace(t1.a,'[[:space:]]|[[:punct:]]','') = t2.x
order by x;

編輯

之前的join條件是錯誤的,因為xselect計算列的別名,所以它在某種程度上處於“表示級別”。 實際的列名仍然是原始列名,您需要在join條件中使用它。 我編輯了我的查詢,現在應該是正確的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM