-- Run in the lakehouse SQL analytics endpoint after publishing a matched release. DECLARE @cohort_end int = (SELECT MAX(cohort_end_year) FROM dbo.nait_cohorts); DECLARE @institution varchar(20) = 'UA'; -- Advising shortlist. Each count is for one external institution one year before NAIT. -- One student may appear against multiple institutions. WITH prior_year AS ( SELECT h.cohort_end_year, h.institution_code, h.institution_name, h.learners, c.cohort_size, CAST(h.learners AS decimal(20, 8)) / c.cohort_size AS cohort_share FROM dbo.nait_history AS h INNER JOIN dbo.nait_cohorts AS c ON c.cohort_end_year = h.cohort_end_year WHERE h.lookback_years = 1 AND h.institution_code <> 'NA' ), ranked AS ( SELECT *, DENSE_RANK() OVER (ORDER BY learners DESC) AS audience_rank FROM prior_year WHERE cohort_end_year = @cohort_end AND learners IS NOT NULL ) SELECT cur.audience_rank, cur.institution_code, cur.institution_name, cur.learners, cur.cohort_size, CAST(100 * cur.cohort_share AS decimal(8, 3)) AS cohort_percent, prev.learners AS previous_cohort_learners, cur.learners - prev.learners AS change_in_learners, CAST(100 * (cur.cohort_share - prev.cohort_share) AS decimal(8, 3)) AS change_percentage_points FROM ranked AS cur LEFT JOIN prior_year AS prev ON prev.cohort_end_year = cur.cohort_end_year - 1 AND prev.institution_code = cur.institution_code ORDER BY cur.audience_rank, cur.institution_name; -- Compare observation years for a single institution and cohort. A lag of zero -- refers to the NAIT cohort year itself. Unavailable estimates remain NULL. SELECT h.institution_name, h.cohort_end_year, h.lookback_years, h.cohort_end_year - h.lookback_years AS observation_year_ending, h.learners, c.cohort_size, CAST(100.0 * h.learners / c.cohort_size AS decimal(8, 3)) AS cohort_percent, h.source_url FROM dbo.nait_history AS h JOIN dbo.nait_cohorts AS c ON c.cohort_end_year = h.cohort_end_year WHERE h.cohort_end_year = @cohort_end AND h.institution_code = @institution ORDER BY h.lookback_years DESC; -- Compare the distinct cohort denominator to the published NAIT current-year cell. -- A release with no missing cells can still omit institutions in some cohort years. SELECT c.cohort_end_year, c.cohort_size, MAX(CASE WHEN h.institution_code = 'NA' AND h.lookback_years = 0 THEN h.learners END) AS nait_current_year, COUNT(DISTINCT h.institution_code) AS institutions_listed, COUNT(h.lookback_years) AS published_cells, COUNT(h.lookback_years) - COUNT(h.learners) AS blank_cells, c.source_url FROM dbo.nait_cohorts AS c LEFT JOIN dbo.nait_history AS h ON h.cohort_end_year = c.cohort_end_year GROUP BY c.cohort_end_year, c.cohort_size, c.source_url ORDER BY c.cohort_end_year;