Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Concatenating the columns of diff rows while selecting

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
gauravgupta2808
Warnings : 1

New User


Joined: 31 May 2007
Posts: 31
Location: Chennai, India

PostPosted: Mon Oct 29, 2007 10:35 pm    Post subject: Concatenating the columns of diff rows while selecting
Reply with quote

Hi,

I have a data like this in

Code:


Sort        Account         FName        SName
1001      10001            F1                S1
1001      10002            F2                S2
1001      10001            F3                S3



I need to select the concatenated FName SName (separated by &) for all similar Sort Account

Like if input is 1001 10001, then output should be:
Code:

1001    10001      F1 S1 & F3 S3


Can you please suggest me some query
Back to top
View user's profile Send private message

dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Tue Oct 30, 2007 1:34 am    Post subject:
Reply with quote

Hello,

What should happen when there 800 rows of the same sort/account values?

What should happen when there are multiples of the same Fname/Sname? Should duplicates be discarded?
Back to top
View user's profile Send private message
gauravgupta2808
Warnings : 1

New User


Joined: 31 May 2007
Posts: 31
Location: Chennai, India

PostPosted: Tue Oct 30, 2007 4:43 am    Post subject: Reply to: Concatenating the columns of diff rows while selec
Reply with quote

Hi,

In an ideal scenario, there will not be mor ethan 5-6 Sortcode & acoount


There will be no similar FName & SName,,, if they do occur, duplicates are not discarded
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 632
Location: Wisconsin

PostPosted: Tue Oct 30, 2007 7:34 am    Post subject:
Reply with quote

Ideal scenarios don't happen.

What about an account held by Albert Smith and his son Albert Smith Jr and his grandson Albert Smith III. Maybe they were got busy at a young age and there is a great grandson named Albert Smith IV?
Back to top
View user's profile Send private message
gauravgupta2808
Warnings : 1

New User


Joined: 31 May 2007
Posts: 31
Location: Chennai, India

PostPosted: Tue Oct 30, 2007 2:11 pm    Post subject: Reply to: Concatenating the columns of diff rows while selec
Reply with quote

In this case, if they hold sortcode 10001 & Account 20001 then it should display

10001 20001 Albert Smith & Albert Smith Jr & Albert Smith III

If great grandson is also there, then:

10001 20001 Albert Smith & Albert Smith Jr & Albert Smith III & Albert Smith IV

PS: The order in which the names are displayed doesnot matter.
Back to top
View user's profile Send private message
stodolas

Active Member


Joined: 13 Jun 2007
Posts: 632
Location: Wisconsin

PostPosted: Tue Oct 30, 2007 5:10 pm    Post subject:
Reply with quote

You can't do that on a select then. Even if you could, if someone had to come back and do maintenance on it, they would want to strangle the person who wrote it. You should do post processing through COBOL or something.

Setup your query with an order by Sort, Account then loop through it using STRING.
Back to top
View user's profile Send private message
zero

New User


Joined: 01 Dec 2007
Posts: 21
Location: Hyderabad

PostPosted: Sat Dec 01, 2007 12:58 pm    Post subject: Reply to: Concatenating the columns of diff rows while selec
Reply with quote

Hi,
if there are only two records for any SORT and ACCOUNT combination, we can use the following query :

SELECT A.SORT, A.ACCOUNT, CONCAT(CONCAT(CONCAT(A.FNAME,A.LASTNAME), '&'),CONCAT(B.FNAME,B.LASTNAME))
FROM SILLY2 A, SILLY2 B
WHERE A.SORT = B.SORT
AND A.ACCOUNT = B.ACCOUNT
AND A.FNAME <> B.FNAME
ORDER BY A.FNAME, A.LASTNAME
Fetch first 1 row only;
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts compare 2 rows within the same file ram_vizag SYNCSORT 7 Wed Jun 14, 2017 12:34 am
No new posts unload data from table with lob columns farhad_evan DB2 1 Sat Apr 22, 2017 1:32 pm
No new posts To display values of multiple rows in... vinu78 DB2 2 Thu Mar 09, 2017 1:20 pm
No new posts how to send just 10 rows in a CICS sc... Megha Gupta CICS 5 Thu Feb 23, 2017 6:57 pm
This topic is locked: you cannot edit posts or make replies. DB2 query Using Recursion, Converting... smilewithashu2 DB2 3 Tue Jan 03, 2017 12:50 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us