2017-04-13 68 views
1

我想用另一个表中的列创建一个新表。只应选择那些行,其中列x具有唯一值。列x应由列a的修整值组成。oracle中的多列表中的一列的唯一值sql

这是我的代码:

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

我如何只包括在列X唯一值的行?

回答

1

您可以加入与另外一个,只返回独特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条件是错误的,因为x是cal的别名在select的culate列,所以它在某种程度上是“演示级别”。实际的列名仍然是原来的名称,您需要使用join条件中的名称。我编辑了我的查询,现在应该是正确的。

+0

谢谢。它不应该从联合括号内的'table1 t1'吗? – yPennylane

+0

谢谢,我编辑它。在内部查询和外部查询中都有相同的别名可能会引起混淆,所以我只是从内部的't1.a'中删除别名,因为这不是必需的。 –

+0

我用我的数据测试了代码,并得到错误'00904。 00000 - “G1”。“X”:g1.x = g2.x'上的无效标识符“'line。x in in clob格式。这就是它不工作的原因吗? – yPennylane