eAIR Newsletter Reformat

  • Tech Tips
  • 07.12.19

Using the Listagg Function in SQL

  • by Jinny Case, Senior Research Analyst and Ashwin Jayagopal, Institutional Research Analyst, University of Texas at San Antonio

Our institutional research analysts frequently extract data and modify the structure of data using SQL to suit the needs of data consumers or to ease analysis. Often, we need to aggregate tables containing multiple rows of data per student or faculty member to one row. Typically, the max, min, or some other aggregating function is used to gather only the necessary data into one row, but how do you handle a situation in which all of the data is needed? This is where the listagg function proves useful.

We use active athletics status as an example here. Student athlete data are considered part of public directory information. Upon receiving public information office (PIO) requests for directory data, we use the listagg function in our PIO code to ensure we include a description of all sports played while maintaining one record per student.

This query joining the Sport Information table in Banner to the Sport validation table serves as our base subquery to extract athletes. Here, we select the person identifier along with term code and sport for all active and fifth year athletes.

TT-July-2019a

In rare instances, student athletes may play more than one sport resulting in duplicate rows for the query. Here is an example of what the extracted data might look like in the case of multi-sport athletes. 

TT-July-2019b

Since we want only one row for each unique student showing all sports played in a given time period, we need to modify the query to use the listagg function, which aggregates the data into one row, separating the data in multiple rows with the delimiter of your choice. Again, we use SGRSPRT as an example:

TT-July-2019c

The query above is aggregating by student identifier to bring in multiple sports for each term, separating the sports with a comma and one space. We will still retain duplicates for athletes playing in multiple terms, but that is all right since we will join this to enrollment data by PIDM and term. If we needed to show sports in the same row in a specific order, say alphabetical, we would simply add STVACTC_DESC in the ORDER BY statement after PIDM. Here is the result of the modified query using the listagg function.

TT-July-2019d

Another example of using the listagg function is when we want to display a student and all the grades that the student received for the courses taken in a term. In a specific term, a student may take more than one course and if we want to see a single record displaying student’s details and their respective grades, we can merge all the grades into a list separating it with any specific delimiter.

Please see the query below :

TT-July-2019e

TT-July-2019f

Back to eAIR Newsletter Reformat

About eAIR

eAIR is the newsletter of the Association for Institutional Research (AIR). From its start in October 1987 to today, eAIR remains one of the most important tools for providing news to the higher education community.

View Articles By Column

Hot Topics

Subscribe

Subscribe to eAIR

Create a free account or update your communication preferences to receive the eAIR newsletter.

Association at a Glance

Featured Opportunities

Did you know?

AIR welcomes brief conference/event announcements of interest to the IR and higher education communities.
Learn More 

AIR Hub

Recent Discussions

Listings Closing Soon

Total jobs: 3
Job Title Institution Location Salary Range Application Due Date Control Column
Title V Data Research Analyst Mount Saint Mary's University, Los Angeles CA Dependent on qualifications and experience 2024-06-30
Director, Academic Effectiveness University of San Diego CA $8583 - $11666 2024-06-07
Data Visualization Specialist Bellevue College WA $73539 - $106632 2024-06-02