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

SQL Concat Issue


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
db2sysdba.zos

New User


Joined: 04 Oct 2011
Posts: 25
Location: INDIA

PostPosted: Tue Feb 14, 2012 1:05 pm
Reply with quote

Input Data:
aaa 3123 dave thomas
bbb 4244 catherine
aaa 2130 david
ccc 4244 herry

Query output should be as :
aaa 3123 dave thomas 2130 david
bbb 4244 catherine
ccc 4244 herry

that the above mentioned input file data is in a Table.
can we get the required above mentioned output from SQL Query?
Back to top
View user's profile Send private message
Nikhil Jain

New User


Joined: 20 Jul 2011
Posts: 16
Location: India

PostPosted: Tue Feb 14, 2012 2:52 pm
Reply with quote

Hi,

You can try writing a query similar to the below given query -

Code:
WITH NIK (RNUM,PSTL_ID,CITY_NAME) AS         
(                                                   
   SELECT ROW_NUMBER() OVER(ORDER BY CITY_NAME DESC), 
   PSTL_ID,CITY_NAME                           
   FROM ABC
   WHERE PSTL_ID='123-456'                     
)                                                   
  SELECT A.PSTL_ID,A.CITY_NAME                 
    ,(                                               
      SELECT B.CITY_NAME                               
      FROM NIK B                                     
      WHERE B.PSTL_ID='123-456'               
      AND RNUM=2                                     
     ) AS COL2                                       
    ,(                                               
      SELECT B.CITY_NAME                               
      FROM NIK B                     
      WHERE B.PSTL_ID='123-456'
      AND RNUM=3                     
     ) AS COL3                       
    ,(                               
      SELECT B.CITY_NAME               
      FROM NIK B                     
      WHERE B.PSTL_ID='123-456'
      AND RNUM=4                     
     ) AS COL4                       
    FROM NIK A                       
    WHERE A.PSTL_ID='123-456' 
    FETCH FIRST 1 ROWS ONLY         


NB : You have to be very sure of the maximum no. of records/rows that can be returned against a particular where clause. In my case, I knew for a particular PSTL_ID, there can be max 4 Cities. Hope it helps!!
Back to top
View user's profile Send private message
sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1020
Location: India

PostPosted: Wed Feb 15, 2012 7:46 pm
Reply with quote

Hey sysdba,

Try this link and see the samples given by GuyC

That should do the trick.

Thanks,
Sushanth
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 SFTP Issue - destination file record ... All Other Mainframe Topics 2
No new posts Issue after ISPF copy to Linklist Lib... TSO/ISPF 1
No new posts Facing ABM3 issue! CICS 3
No new posts Panvalet - 9 Character name - Issue c... CA Products 6
No new posts Issue with EXEC CICS QUERY SECURITY c... CICS 6
Search our Forums:

Back to Top