How to concatenate multiple rows into a single row using SQL
To do this, there is GROUP_CONCAT function
in MySQL. But unfortunately we do not have that function in DB2. So what we can
do is create our own SQL function.
One day my superior showed me a simple SQL query he executed. That query was
listed all the claim types that each policy holder had. The result set was as
below.
Plcy_Num
|
Plcy_Type
|
SSD0113
|
AA
|
SSD0113
|
MM
|
SSD1171
|
AA
|
SSD1171
|
MM
|
SSD1998
|
MM
|
SSD3084
|
SS
|
SSD5818
|
MM
|
SSD6722
|
MM
|
SSD7843
|
AA
|
SSD7843
|
MM
|
The he had executed below query.
select Plcy_Num, Plcy_Type from Library.claim_table group by Plcy_Num, Plcy_Type order by Plcy_Num, Plcy_Type |
My superior asked me “Poorna, is there any way to get all the claim types into
a single row against particular policy number?” I said “Yes. That should be possible.
Give me few minutes I’ll try and let you know”
Then I googled and found that I can achieve this task by
creating a simple SQL function.
1st Step
Type STRSQL command in the command line. (The only command that I type most number
of times in a day)
2nd Step
Let’s create the SQL function