IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

All names in a dept in one row


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Bharath Nadipally

New User


Joined: 24 Jun 2008
Posts: 22
Location: Hyderabad

PostPosted: Sun Mar 29, 2009 5:09 pm
Reply with quote

Hi,
Below are the records in a table.

Dept name
------ -------
10 n1
10 n2
10 n3
20 n4
20 n5

required o/p:
10 n1,n2,n3
20 n4,n5

Is it possible to get required output using one sql query?
Back to top
View user's profile Send private message
kregen

New User


Joined: 16 Mar 2006
Posts: 21

PostPosted: Mon Mar 30, 2009 2:21 pm
Reply with quote

hi Bharath Nadipally,

Tabledefinition
Code:
  DECLARE GLOBAL TEMPORARY TABLE SESSION.DEPTTMP 
    (DEPT    SMALLINT NOT NULL                   
    ,NAME    CHAR(5)  NOT NULL                   
    )                                             
  ;                                               
                                                 


Testdata
Code:

  INSERT INTO SESSION.DEPTTMP VALUES(10, 'N1');   
  INSERT INTO SESSION.DEPTTMP VALUES(10, 'N2');   
  INSERT INTO SESSION.DEPTTMP VALUES(10, 'N3');   
  INSERT INTO SESSION.DEPTTMP VALUES(10, 'N4');   
  INSERT INTO SESSION.DEPTTMP VALUES(10, 'N5');   
  INSERT INTO SESSION.DEPTTMP VALUES(10, 'N6');   
                                                 
  INSERT INTO SESSION.DEPTTMP VALUES(20, 'N1');   
  INSERT INTO SESSION.DEPTTMP VALUES(20, 'N2');   
  INSERT INTO SESSION.DEPTTMP VALUES(20, 'N3');   
                                                 
  INSERT INTO SESSION.DEPTTMP VALUES(30, 'N4');   
  INSERT INTO SESSION.DEPTTMP VALUES(30, 'N5');   
  INSERT INTO SESSION.DEPTTMP VALUES(30, 'N6');


And now the SQL-Statment
Code:

 WITH TEMP1 (DEPT, W#, NAME, ALL_NAME) AS             
      (SELECT DEPT                                     
             ,SMALLINT(1)                             
             ,MIN(NAME)                               
             ,VARCHAR(MIN(STRIP(NAME,B,' ')), 4096)   
         FROM SESSION.DEPTTMP A                       
        GROUP BY DEPT                                 
      UNION ALL                                       
        SELECT A.DEPT                                 
              ,SMALLINT(B.W#+1)                       
              ,A.NAME                                 
              ,B.ALL_NAME || ',' || STRIP(A.NAME,B,' ')
          FROM SESSION.DEPTTMP A                       
              ,TEMP1 B                                 
         WHERE A.DEPT = B.DEPT                         
           AND A.NAME > B.NAME                         
           AND A.NAME = (SELECT MIN(C.NAME)           
                           FROM SESSION.DEPTTMP C     
                          WHERE C.DEPT = B.DEPT       
                            AND C.NAME > B.NAME)       
      )                                               
 SELECT DEPT                                           
       ,ALL_NAME                                       
   FROM TEMP1 D                                       
    WHERE W# = (SELECT MAX(W#)                         
                  FROM TEMP1 E                         
                 WHERE D.DEPT = E.DEPT)               
  ORDER BY DEPT;                                       


a little bit longer... but one statment

Warning: this statment is very slow! don't use it when you have a big table with many rows

cu
kregen
Back to top
View user's profile Send private message
Bharath Nadipally

New User


Joined: 24 Jun 2008
Posts: 22
Location: Hyderabad

PostPosted: Tue Mar 31, 2009 10:35 am
Reply with quote

hi kregen,

Thank you very much.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Capturing COBOL job and program names... All Other Mainframe Topics 2
No new posts Read file names from existing file th... DFSORT/ICETOOL 6
No new posts Unable to retrieve Datasets Names usi... CLIST & REXX 20
No new posts Column names in SYSIBM tables DB2 5
No new posts Fetch the Dataset names from inside m... TSO/ISPF 18
Search our Forums:

Back to Top