Aggregate Data Into a Single Column
Use LISTAGG as a query expression, to string several rows into a single row, in a single column.
This is a trick for PSQuery, for Oracle Databases only (at this time). We’ll be using the LISTAGG function. Read more about it at
https://www.geeksforgeeks.org/sql-listagg/
In Query Expression, you can add this LISTAGG function.
The below is an example for the Vendor Address phone number table in PeopleSoft FSCM. The intended desire is to display each SETID and VENDOR_ID on its own row, but only once per combination. Then we want a third field to put all phone numbers into a single column. If a vendor has 1 or 20 phone numbers, we just want one row of data, but list all phone numbers in the third field in our results.
Steps to produce this in FSCM
- Create a new Query in Query Manager.
- Insert record VENDOR_ADDR_PHN.
- The Effective Dated logic will be automatically added. This is fine.
- Select the following fields to be displayed: SETID and VENDOR_ID.
- Use the checkboxes on the Query tab.
- Go to the Expressions tab.
- Create a new Expression that has the following (screenshot found later in this post):
- Expression Type: Long Character
- Aggregate Function: Turn this checkbox on.
- Expression Text: LISTAGG(A.PHONE, ‘ , ‘) WITHIN GROUP (ORDER BY A.PHONE)
- (If you are using this trick with other tables already added, be sure to use the proper alias and field name. Our example assumes the field PHONE is from record alias “A”.)
- Click OK, to save the new expression.
- Choose “Use as Field“, for our new expression.
- This adds our new expression to the Fields tab.
- Modify any heading text on the Fields tab.
- Run to view results.
- You should find that each SETID and VENDOR_ID have their own row, with the third field (Phone) having 1 or more phone numbers, separated by commas.
Bonus
You can also try out this Expression Text. It adds the Phone Type.
LISTAGG(A.PHONE_TYPE %CONCAT ':' %CONCAT A.PHONE, ' , ') WITHIN GROUP (ORDER BY A.PHONE_TYPE %CONCAT ':' %CONCAT A.PHONE)