PS Query & SQL

 View Only
Expand all | Collapse all

Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

  • 1.  Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

    Posted 01-31-2025 04:26 PM
    Edited by Daniel Labrecque 01-31-2025 05:15 PM
      |   view attached

    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.
    ------------------------------

    Attachment(s)

    Alliance 2026 Registration is Open!


  • 2.  RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

    Posted 02-02-2025 08:28 PM

    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.
    ------------------------------

    Alliance 2026 Registration is Open!


  • 3.  RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

    Posted 02-03-2025 12:32 AM

    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.
    ------------------------------

    Alliance 2026 Registration is Open!


  • 4.  RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

    Posted 02-03-2025 08:24 AM

    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.
    ------------------------------

    Alliance 2026 Registration is Open!


  • 5.  RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

    Posted 02-03-2025 11:44 AM

    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.
    ------------------------------

    Alliance 2026 Registration is Open!


  • 6.  RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

    Posted 02-03-2025 12:07 PM
    ...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



    David Ehrlich | Senior Business System Analyst - Admissions, Student Records, Data & Reporting

    Duke University | Student Information Services & System (SISS)

    david.ehrlich@duke.edu | 919-684-1270

    www.sissoffice.duke.edu





    Alliance 2026 Registration is Open!


  • 7.  RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

    Posted 02-03-2025 01:13 PM

    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.
    ------------------------------

    Alliance 2026 Registration is Open!


  • 8.  RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

    Posted 02-03-2025 01:33 PM
    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?  




    David Ehrlich | Senior Business System Analyst - Admissions, Student Records, Data & Reporting

    Duke University | Student Information Services & System (SISS)

    david.ehrlich@duke.edu | 919-684-1270

    www.sissoffice.duke.edu





    Alliance 2026 Registration is Open!


  • 9.  RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

    Posted 02-03-2025 01:37 PM

    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.
    ------------------------------

    Alliance 2026 Registration is Open!


  • 10.  RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

    Posted 02-03-2025 04:15 PM
    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



    Alliance 2026 Registration is Open!


  • 11.  RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

    Posted 02-03-2025 01:34 PM

    Yes, Scott.  My example in the document deals with my student grant query. This uses the STDNT_AWARDS table which works well because each row has a unique packaging sequence.  Say my SQL has a space for three columns of grants and student A has three and student B has 2, the last column for student B is NULL.  If I want, I can then use a NVL expression to make sure any NULL values are zero.



    ------------------------------
    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.
    ------------------------------

    Alliance 2026 Registration is Open!


  • 12.  RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

    Posted 02-04-2025 07:59 AM

    Thank you for this information!

    Can anyone provide an example of when it makes sense to use the Nth value with Student Records, like enrollment or program/plan?



    ------------------------------
    Margie Monforton
    Business Systems Analyst
    University of Michigan
    ------------------------------

    Message from the HEUG Marketplace:
    ------------------------------
    Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
    ------------------------------

    Alliance 2026 Registration is Open!


  • 13.  RE: Expanding Your SQL Toolkit: NTH_VALUE for Flattening Student Data

    Posted 02-04-2025 08:43 AM
    Here is an example of Plan data
    ID
    Career
    Career Nbr
    Acad Prog
    Major
    Minor
    Minor
    Minor
    Minor
    123456789
    GRAD
    0
    GRED
    EDPSY_PHD
    CIED_GDMN
    STAT_GDMN
     
    nth_value(B.ACAD_PLAN,1) over (partition by B.EMPLID,B.ACAD_CAREER,B.STDNT_CAR_NBR order by B.PLAN_SEQUENCE RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)

    where B is ACAD_PLAN and ,1 is Major ,2 is Minor(1) and ,3 is Minor(2)

    Hope this helps,
    Bob
    --
    Bob Fogarty
    LionPATH - Reporting Team Lead
    The Pennsylvania State University
    rtf12@psu.edu



    Alliance 2026 Registration is Open!