FIRST_VALUE is a variation of Nth value that shows the Nth value of 1 and can be very useful without worry of duplication and partitions because first is always first!!!
I use this to determine primary plan based on the order by clause. Imagine ordering your data in excel using asc/desc sorts to get the first row ... order by clause determines FIRST_VALUE.
Nice thing about these functions is PS Query doesn't blink with expressions, the joins cause performance issues.
Bob
--
Bob Fogarty
LionPATH - Reporting Team Lead
The Pennsylvania State University
rtf12@psu.edu
Original Message:
Sent: 2/3/2025 1:37:00 PM
From: Daniel Labrecque
Subject: RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data
David,
Indeed exceeding that fixed value of columns is possible. It is important to know your data. So what I did is run queries wide open for each category of award I had to see what were the most columns I would get for each category. This is also why I created different queries for different categories and used them in a composite query and connected query.
------------------------------
Daniel Labrecque
Senior 2 Business Systems Analyst/Functional Architect
University of Nevada, Las Vegas
------------------------------
Message from the HEUG Marketplace:
------------------------------
Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
------------------------------
Original Message:
Sent: 02-03-2025 01:32 PM
From: David Ehrlich
Subject: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data
NULL is a "special" (non)-value.
I played with the SQL in SQLDev. When I check for "IS NULL" (in place of <> ' ') I get the "Yes" at the "wrong time".
You certainly could be "more complete" and check for NULL in addition.
The main point of my note being an approach to: what do you do if the fixed number of columns is exceeded?
Original Message:
Sent: 2/3/2025 1:13:00 PM
From: Scott Nishizaki
Subject: RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data
Pardon what may be naivety on my part, but I though that if there was no n value that NTH_VALUE returned NULL.
Tech On the Net:
Returns
The NTH_VALUE function returns the nth value in an ordered set of values from an analytic window.
If there are less than n rows in the data source window, the NTH_VALUE function will return NULL.
If the parameter n is NULL, the NTH_VALUE function returns an error.
Wouldn't NULL <> ' ' return TRUE?
------------------------------
Scott Nishizaki
Developer/Analyst
Azusa Pacific University
------------------------------
Message from the HEUG Marketplace:
------------------------------
Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
Original Message:
Sent: 02-03-2025 12:06 PM
From: David Ehrlich
Subject: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data
...regarding the usage and fixed-nature of output columns of NTH_VALUE...
I recently completed a report which lists course enrollments for a population of students. There are 8 courses which are currently selected for inclusion in the report. It is expected that students will be in at most three of those courses.
Therefore, we provided three sets of columns of data fields using NTH_VALUE to consolidate each student into one row.
That lead to the question: how do you know if a student has more than three courses?
To address this case, I check for this case, using NTH_VALUE, and added the result to the output. That gives the user the visual clue that the data-limits have been exceeded.
Here is the expression for those that are interested ('Yes' is displayed when this situation appears, otherwise the column is blank):
CASE
WHEN NTH_VALUE (A.CRSE_ID, 4)
IGNORE NULLS
OVER (PARTITION BY B.EMPLID ORDER BY A.SUBJECT, A.CATALOG_NBR
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) <> ' '
THEN 'Yes'
ELSE ' '
END
Original Message:
Sent: 2/3/2025 11:44:00 AM
From: Scott Nishizaki
Subject: RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data
In my experience, the Key Values of the records are usually the biggest consideration when creating a partition. Remember that Key Values, from a database standpoint, are what identify unique "rows" of data. If you were to include all of the keys from all of the records in your partition, you could guarantee unique Nth values. That said, building your partition that way would (almost certainly) break your "Select Distinct" but partitioning with that in mind helps to understand why something might be both the Nth and N+1th value or not.
------------------------------
Scott Nishizaki
Developer/Analyst
Azusa Pacific University
------------------------------
Message from the HEUG Marketplace:
------------------------------
Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
Original Message:
Sent: 02-03-2025 08:23 AM
From: Robert Fogarty
Subject: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data
incorporating an example would be fantastic ... the difficulty is that we often don't identity that a duplicate will occur, the data does and it is often a surprise that we handle with the select distinct. The key is to make it known/shown when using nth value.
------------------------------
Robert Fogarty PMP
Reporting Team Lead | LionPATH Development and Maintenance Office
The Pennsylvania State University
------------------------------
Message from the HEUG Marketplace:
------------------------------
Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
Original Message:
Sent: 02-03-2025 12:32 AM
From: Daniel Labrecque
Subject: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data
Robert,
Do you think I should add anything to the tutorial? I am open to making changes so everyone can gain additional information. Let me know.
------------------------------
Daniel Labrecque
Senior 2 Business Systems Analyst/Functional Architect
University of Nevada, Las Vegas
------------------------------
Message from the HEUG Marketplace:
------------------------------
Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
Original Message:
Sent: 02-02-2025 08:28 PM
From: Robert Fogarty
Subject: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data
nth value is very useful ... the key is to properly define the partition. There is no distinct nth value and if your data contains duplicate rows and is not properly partitioned nth value 1 will be the same as nth value 2
------------------------------
Robert Fogarty PMP
Reporting Team Lead | LionPATH Development and Maintenance Office
The Pennsylvania State University
------------------------------
Message from the HEUG Marketplace:
------------------------------
Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
Original Message:
Sent: 01-31-2025 04:25 PM
From: Daniel Labrecque
Subject: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data
Understanding SQL window functions can greatly improve how we work with datasets, especially when multiple records exist for a single entity, such as students.
I've created a tutorial on using NTH_VALUE, a powerful analytic function that allows you to retrieve the nth record from an ordered partition. While this example is based on financial aid data, the same technique can be applied to any area where multiple rows of data need to be transformed into a single-row format per student.
What's in the tutorial?
- Introduction to NTH_VALUE and how it works
- SQL query example that flattens student grant data
- Step-by-step instructions for PS Query implementation
- Key takeaways for applying NTH_VALUE to different use cases
This function is particularly useful when working with financial aid awards, loans, scholarships, academic records, enrollments, and other student data where multiple entries exist per student.
I've attached the full tutorial as a PDF-feel free to download it and test it out!
If you have questions or want to discuss other SQL window functions that help with student data reporting, drop a comment below! I am always open to updating any documents I have posted if they can be improved.
------------------------------
Daniel Labrecque
Senior 2 Business Systems Analyst/Functional Architect
University of Nevada, Las Vegas
------------------------------
Message from the HEUG Marketplace:
------------------------------
Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
------------------------------