Nested loops in SQL Server
Posted on 2013-06-04
I have a table which contains among other fields: sites, collection years, treatments, samples, species, and counts of seedlings.
I want to be able to loop through the table and select the 5 most abundant species grouped by: sites, collection years, treatments,species.
In the t-sql script that I have created, I have been unable to get the looping to work correctly. The outer loop ( cycling thru sites) is not being executed.
My intention is for the inner loop to cycle through treatments, the middle loop cycles through years and the outer loop cycles through sites.
I have attached a zip of the sql script and the results of the script as a csv file.