我想用另一个表中的列创建一个新表。只应选择那些行,其中列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中包含具有唯一值的行?
答案 0 :(得分: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
是select
中计算列的别名,所以它在某种程度上处于“演示级别”。实际的列名仍然是原始列名,您需要在join
条件下使用它。我编辑了我的查询,现在应该是正确的。