Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking for a function(s) that will provide something similar to GROUP_CONCAT() or LISTAGG()

Status
Not open for further replies.

derkenstock

Programmer
Jan 10, 2019
1
US
Essentially I'm attempting to accomplish concatenating data in a field for records that share the same key in a particular table, as shown below.
I've seen the GROUP_CONCAT(), and LISTAGG() listed on other similar questions, and I am familiar with implementations that would solve the situation below but it seems like PervasiveSQL 13 does not have support for these functions. I am wondering if Pervasive supports anything that I could use to solve my issue.


Code:
    id       colour

1        red
1        blue
2        green
2        red

Either of the following solutions would be acceptable for my use case, but the first one would be more preferable.

Code:
    id     colour

1      red, blue
2      green, red

OR

Code:
    id     colour1    colour2

1      red        blue
2      green      red
 
I'm not aware of a single SQL statement method of pivoting the table like you want. You might be able to create a function that generates the flattened column (colour) and use that function in the select statement. You might have to do what you are wanting in code.

Something like this:
Code:
create function udfFlattenRow(IN :ID int)
returns char(500)
as 
begin
  declare :result char(500);
  declare :loopvalue char(10);
  DECLARE c1 CURSOR FOR SELECT color FROM ttGroup where id = :ID FOR READ ONLY;
  OPEN c1;
  BulkLinesLoop:
  LOOP
    FETCH NEXT FROM c1 INTO :loopvalue;
    set :result = :result + ',' + :loopvalue; 
    IF SQLSTATE = '02000' THEN
      LEAVE BulkLinesLoop;
    END IF;       
  END LOOP;
  CLOSE c1;
  return :result;
end;
I have not tested the function so I'm not sure if it works.


Mirtheil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top