Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
843 views
in Technique[技术] by (71.8m points)

oracle - Inner Join Two Table, aggregating varchar fields

Sorry I change my question a bit . I have two table and I want to merge them

  TERMS_TABLE

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

TFIDF_TABLE

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

Now I want to add TERMS column of TERMS_TABLE to terms column of TFIDF_TABLE like that

result should be:

NEW_TFIDF_TABLE

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

I try this code:

Insert into    NEW_TFIDF_TABLE
SELECT T.ID, T.TERMS ||', '|| TT.TERMS FROM TFIDF_TABLE T
INNER JOIN TERMS_TABLE TT  ON  T.ID=TT.ID

but it does not do what I want How can I do that ?

Modification: I forgot an issue. If TFIDF_TABLE already contains TERMS of TERMS_TABLE then I should not insert this term to the NEW_TFIDF_TABLE. To do this control mechanism what should I do ?

Example :

TERMS_TABLE

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



TFIDF_TABLE

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

RESULT:

NEW_TFIDF_TABLE

  ID | TERMS
 309 |'computer,phone,mp3....,hardware,software'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you got Oracle 11g, you can use the LISTAGG function, like so:

Insert into    NEW_TFIDF_TABLE
SELECT T.ID, T.TERMS ||', '|| LISTAGG(TT.TERMS, ',') 
FROM TFIDF_TABLE T
INNER JOIN TERMS_TABLE TT  ON  T.ID=TT.ID
GROUP BY t.ID, t.TERMS /* assuming the id is unique in t */

If you're on an earlier version, you can create your own aggregate functions using a package. I've build this FNC_CONCATCOMMASEPARATED function, which I freqently use myself.

CREATE OR REPLACE TYPE "AT_CONCATCOMMASEPARATED" as object
(
  V_CONCAT varchar2(32767), -- concatenated strings

  static function ODCIAggregateInitialize(
    P_CONTEXT in out AT_CONCATCOMMASEPARATED)
  return number,

  member function ODCIAggregateIterate(
    self  in out AT_CONCATCOMMASEPARATED,
    P_VALUE in     varchar2)
  return number,

  member function ODCIAggregateTerminate(
    self     in  AT_CONCATCOMMASEPARATED,
    P_RESULT out varchar2,
    P_FLAGS  in  number)
  return number,

  member function ODCIAggregateMerge(
    self      in out AT_CONCATCOMMASEPARATED,
    P_CONTEXT in     AT_CONCATCOMMASEPARATED)
  return number
);

create or replace type body AT_CONCATCOMMASEPARATED is

static function ODCIAggregateInitialize(
  P_CONTEXT in out AT_CONCATCOMMASEPARATED)
return number
is
begin
  if P_CONTEXT is null then
    P_CONTEXT := AT_CONCATCOMMASEPARATED('');
  else
    P_CONTEXT.V_CONCAT := null;
  end if;

  return ODCIConst.Success;
end;

member function ODCIAggregateIterate(
  self    in out AT_CONCATCOMMASEPARATED,
  P_VALUE in     varchar2)
return number
is
begin
  if self.V_CONCAT is null then
    self.V_CONCAT := P_VALUE;
  else
    self.V_CONCAT := self.V_CONCAT || ', ' || P_VALUE;
  end if;

  return ODCIConst.Success;
end;

member function ODCIAggregateTerminate(
  self     in  AT_CONCATCOMMASEPARATED,
  P_RESULT out varchar2,
  P_FLAGS  in  number)
return number
is
begin
  P_RESULT := substr(self.V_CONCAT, 0, 4000);

  return ODCIConst.Success;
end;

member function ODCIAggregateMerge(
  self      in out AT_CONCATCOMMASEPARATED,
  P_CONTEXT in     AT_CONCATCOMMASEPARATED)
return number
is
begin
  if P_CONTEXT.V_CONCAT is not null then
    if self.V_CONCAT is null then
      self.V_CONCAT := P_CONTEXT.V_CONCAT;
    else
      self.V_CONCAT := self.V_CONCAT || ', ' || P_CONTEXT.V_CONCAT;
    end if;
  end if;

  return ODCIConst.Success;
end;

end;

create or replace function FNC_CONCATCOMMASEPARATED (input varchar2) return varchar2
aggregate using AT_CONCATCOMMASEPARATED;

The insert statement then becomes:

Insert into    NEW_TFIDF_TABLE
SELECT T.ID, T.TERMS ||', '|| FNC_CONCATCOMMASEPARATED(TT.TERMS) 
FROM TFIDF_TABLE T
INNER JOIN TERMS_TABLE TT  ON  T.ID=TT.ID
GROUP BY t.ID, t.TERMS /* assuming the id is unique in t */

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...