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

Concatenating the columns of diff rows while selecting


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
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
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

Moderator Emeritus


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

PostPosted: Tue Oct 30, 2007 1:34 am
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
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
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
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
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
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts To get the count of rows for every 1 ... DB2 3
No new posts Remote Unload of CLOB Columns DB2 6
No new posts Increase the number of columns in the... IBM Tools 3
No new posts Exclude rows with > than x occurre... DFSORT/ICETOOL 6
No new posts Convert single row multi cols to sing... DFSORT/ICETOOL 6
Search our Forums:

Back to Top