This will do it...--===== Create and populate a test table to hold test data -- This is NOT part of the solutionDECLARE @Marks TABLE (Student VARCHAR(10), Marks INT) INSERT INTO @Marks (Student, Marks) SELECT 'Student1',40 UNION ALL SELECT 'Student2',46 UNION ALL SELECT 'Student3',55 UNION ALL SELECT 'Student4',58 UNION ALL SELECT 'Student5',60 UNION ALL SELECT 'Student6',78 UNION ALL SELECT 'Student7',80 UNION ALL SELECT 'Student8',89--===== Declare variable to remember how many studentsDECLARE @StudentCount DECIMAL(9,2)--===== Prepare a working tableDECLARE @Quartile TABLE ( Rank INT IDENTITY(1,1), Student VARCHAR(10), Marks INT )--===== Populate the working table in the correct order INSERT INTO @Quartile (Student, Marks) SELECT Student, Marks FROM @Marks ORDER BY Marks DESC--===== Remember how many students there are SELECT @StudentCount = @@ROWCOUNT--===== Calulate the Quartile for each student SELECT *,CEILING((Rank/@StudentCount*100)/25) AS Quartile FROM @Quartile
--Jeff Moden