Concatenate multiple columns into one columns with comma sep

Ask question on - DataBase Concepts, IBM Mainframe DB2, DB2/UDB, DB2 Tools, SQL , DB2 programming with languages like COBOL, PL1.

Moderators: Kalicharan, Moderator Group

Post Reply
sandeepforever007
Member
Posts: 10
Joined: Wed Apr 02, 2014 9:59 pm

Concatenate multiple columns into one columns with comma sep

Post by sandeepforever007 » Wed May 07, 2014 9:16 am

Hi ,

We have a table in the following format .

Code: Select all


EMPNO  EMPNAME   IND1  IND2   IND3  SALARY
1        x        y    n       y     500
2        y        n    y       n     501
3        z        n    n       y     302
4        p        n    n       n     210


Desired OUTPUT  : 


1,x,"IND1,IND3",500
2,Y,"IND2",501
3,Z,"IND3",302
4,P,"",210

     
COnditions :

1. IF IND is Y , then we have to display the column name
2. IF IND is N , We simply needs to skip .
COuld you please help us in getting the desired output throug Sql statement .


With regards
Sandeep

dhiraj
Member
Posts: 48
Joined: Tue May 06, 2014 11:22 am

Post by dhiraj » Wed May 07, 2014 1:01 pm

Why don't you use simple fetch and use if else in program?
or you can try for CASE during SELECT.


Thanks,
--Dhiraj
Thanks,
--Dhiraj Mishra

NicC
Active Member
Posts: 650
Joined: Sun Jul 24, 2011 5:27 pm
Location: Down on the pig farm

Post by NicC » Wed May 07, 2014 2:40 pm

select col1, ",", col2, ",",Case ind1 when 'y' then 'IND1' else "" End....etc
Regards
Nic

sandeepforever007
Member
Posts: 10
Joined: Wed Apr 02, 2014 9:59 pm

Post by sandeepforever007 » Wed May 07, 2014 4:14 pm

HI Nic,
I have tried out the way you mentioned.
there is a problem with the comma.

Code: Select all

SELECT col1,col2,
'"'||
CASE IND1 WHEN 'Y' THEN 'IND1,'
                  WHEN 'N' THEN '' 
END
CONCAT
CASE IND2 WHEN 'Y' THEN 'IND2,'
                  WHEN 'N' THEN ''
END
||'"', col4
when IND1 is Y and IND2 is N then i am getting a comma after IND1 which shouldnt come.

Code: Select all

OUTPUT:
1,x,"IND1,"500
Could you please help me out

Thanks
Sandeep

NicC
Active Member
Posts: 650
Joined: Sun Jul 24, 2011 5:27 pm
Location: Down on the pig farm

Post by NicC » Wed May 07, 2014 6:15 pm

when IND1 is Y and IND2 is N then i am getting a comma after IND1 which shouldnt come.
because you are coding the comma as part of the WHEN IND1 = 'Y'.

I am by no means an expert on SQL. I would have to create a table and experiment but you can do that. If you want to play at home then download SQLite, or similar, to your own computer and experiment. Or you could wait until someone who knows comes along.
Regards
Nic

NicC
Active Member
Posts: 650
Joined: Sun Jul 24, 2011 5:27 pm
Location: Down on the pig farm

Post by NicC » Wed May 07, 2014 9:42 pm

To get what you want for your indicators:

Code: Select all

CASE col3||col4||col5
   WHEN 'yyy' THEN '"IND1,IND2,IND3"'
   WHEN 'yyn' THEN '"IND1,IND2"'
   WHEN 'ynn' THEN '"IND1"'
   WHEN 'yny' THEN '"IND1,IND3"'
    etc until all combinations covered
END
Note: || is the concatenation symbol in SQLite
Regards
Nic

sandeepforever007
Member
Posts: 10
Joined: Wed Apr 02, 2014 9:59 pm

Post by sandeepforever007 » Thu May 08, 2014 8:25 am

Hi Nic,

We can not go with the suggested solution , Because we are having 11 Indicator variables hence writing combinations is very difficult .

As this req is very much needed for us . Could you pls suggest an appropriate Query for the same .

With Regards
Sandeep

NicC
Active Member
Posts: 650
Joined: Sun Jul 24, 2011 5:27 pm
Location: Down on the pig farm

Post by NicC » Thu May 08, 2014 12:45 pm

Why not let DB2 do what it does best - get the data for you? Then let the program that issued the SELECT do what the program does best - manipulate data. Return all 11 indicators and have the program analyse them and create the last bit of your puzzle.
Regards
Nic

Post Reply

FREE TUTORIALS

Tutorials
Free tutorials from mainframegurukul
  • JCL Tutorial
    Covers all important JCL concepts.
  • Cobol Tutorial
    This tutorials covers all Cobol Topics from STRING to COMP-3.
  • DB2 Tutorial
    DB2 Tutorial focuses on DB2 COBOL Programming.
  • SORT Tutorial
    This Tutorial covers all important aspects of DFSORT with examples
  • CICS Tutorial
    This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming.
Interview
Mainframe Interview questions



Other References
Mainframe Tools and others