DBAsupport.com Forums - Powered by vBulletin
Results 1 to 3 of 3

Thread: query to concatenate three record in one row

  1. #1
    Join Date
    Feb 2006
    Posts
    86

    query to concatenate three record in one row

    i want to concatenate values of three column in one row

    for example my data is:
    Binoy> select * from fpfm_nominies where emp_no='1283L';

    EMP_NO NOMINEE
    ------- -------------------------
    1283L Lal Mathi
    1283L POONAM KUMARI
    1283L SUMAN KUMARI .
    it generates three different records with same emp_no but different
    nominee. now i want to show names of all nominee in one row through select query only.something like this:

    emp_no name
    -------- -------------------------------------------
    1283L Lal Mathi,POONAM KUMARI,SUMAN KUMARI


    can anyone help me out.

  2. #2
    Join Date
    Jul 2002
    Location
    Lake Worth, FL
    Posts
    1,492

    Thumbs down


    Similar questions have been asked time and again, why dont you search first?

    Read this
    "The person who says it cannot be done should not interrupt the person doing it." --Chinese Proverb

  3. #3
    Join Date
    Feb 2003
    Location
    London
    Posts
    170
    This is taken from AskTom.
    Code:
    create or replace type string_agg_type as object
     (
         total varchar2(4000),
      
        static function
              ODCIAggregateInitialize(sctx IN OUT string_agg_type )
             return number,
      
        member function
              ODCIAggregateIterate(self IN OUT string_agg_type ,
                                   value IN varchar2 )
              return number,
      
         member function
              ODCIAggregateTerminate(self IN string_agg_type,
                                     returnValue OUT  varchar2,
                                     flags IN number)
              return number,
      
         member function
              ODCIAggregateMerge(self IN OUT string_agg_type,
                                 ctx2 IN string_agg_type)
              return number
      );
     
    create or replace type body string_agg_type
      is
      static function ODCIAggregateInitialize(sctx IN OUT string_agg_type)
      return number
      is
      begin
          sctx := string_agg_type( null );
          return ODCIConst.Success;
      end;
      
      member function ODCIAggregateIterate(self IN OUT string_agg_type,
                                           value IN varchar2 )
      return number
      is
      begin
          self.total := self.total || ',' || value;
          return ODCIConst.Success;
      end;
      
      member function ODCIAggregateTerminate(self IN string_agg_type,
                                             returnValue OUT varchar2,
                                             flags IN number)
       return number
       is
       begin
           returnValue := ltrim(self.total,',');
          return ODCIConst.Success;
       end;
       
       member function ODCIAggregateMerge(self IN OUT string_agg_type,
                                          ctx2 IN string_agg_type)
       return number
       is
       begin
           self.total := self.total || ctx2.total;
           return ODCIConst.Success;
       end;
        
       end;
     
    
     CREATE or replace
       FUNCTION stragg(input varchar2 )
        RETURN varchar2
        PARALLEL_ENABLE AGGREGATE USING string_agg_type;
    And then if you run the query as
    select emp_no, stragg(nominee)
    from fpfm_nominies
    group by emp_no;

    you should get your desired result.
    thomasp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width