Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Inner Join Two Table, menggabungkan bidang varchar

Jika Anda memiliki Oracle 11g, Anda dapat menggunakan fungsi LISTAGG, seperti:

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 */

Jika Anda menggunakan versi yang lebih lama, Anda dapat membuat fungsi agregat Anda sendiri menggunakan package.json. Saya telah membangun fungsi FNC_CONCATCOMMASEPARATED ini, yang sering saya gunakan sendiri.

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;

Pernyataan insert kemudian menjadi:

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 */


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Mengapa pembaruan massal templat hibernasi ini tidak berfungsi

  2. Menggunakan REGEXP_SUBSTR dengan Strings Qualifier

  3. Ubah judul halaman berdasarkan item di Oracle APEX 4.0

  4. Sesi alter memperlambat kueri melalui Hibernate

  5. Menghapus jam tumpang tindih dari hitungan hari tidak bekerja