应插入聚合列但不同的术语

时间:2011-10-25 08:22:40

标签: oracle distinct aggregate string-aggregation

我有两个表,我想合并它们

TERMS_TABLE

 ID  | TERMS
 309 | 'hardware'
 309 | 'software'
 309 | 'computer'



TFIDF_TABLE

  ID | TERMS
 309 |'computer,phone,mp3....'

现在我想将TERMS_TABLE的TERMS列添加到TFIDF_TABLE的术语列,但如果TFIDF_TABLE已经包含TERMS_TABLE的TERMS,那么我不应该将此术语插入NEW_TFIDF_TABLE,就像那样

结果应为:

NEW_TFIDF_TABLE

  ID | TERMS
 309 |'computer,phone,mp3....,hardware,software'

我该怎么做?

1 个答案:

答案 0 :(得分:2)

如果您使用Oracle 11,可以试试这个:

select t3.id, t3.terms||','||t4.terms terms from 
(
select t1.id, listagg(t1.terms,',') within group (order by t1.terms)  terms
from terms_table t1 join tfidf_table t2 on  t1.id=t2.id
where instr(t2.terms,t1.terms)=0 
group by t1.id ) 
t3 right outer join tfidf_table  t4 on t3.id=t4.id

在Oracle 10上,您可以尝试

select t3.id, t3.terms||','||t4.terms terms from 
(
select t1.id, wm_concat(t1.terms)  terms
from terms_table t1 join tfidf_table t2 on  t1.id=t2.id
where instr(t2.terms,t1.terms)=0 
group by t1.id ) 
t3 right outer join tfidf_table  t4 on t3.id=t4.id