Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to do 'grading' in pure (i.e. ANSI) SQL
    primarykey
    data
    text
    <p>I have a table that looks something like this:</p> <p>CREATE TABLE student_results(id integer, name varchar(32), score float);</p> <p>Lets make the following two assumptions:</p> <ol> <li>assume that the score goes from 0 to a maximum of 100. </li> <li>assume that I want to grade students in 'step sizes' of 10</li> </ol> <p>so I want to apply the following grading:</p> <pre><code>Score Grade Awarded 0-10 GRADE9 10-20 GRADE8 20-30 GRADE7 30-40 GRADE6 40-50 GRADE5 50-60 GRADE4 60-70 GRADE3 70-80 GRADE2 80-90 GRADE1 99-100 GENIUS </code></pre> <p>I would like to write an SQL query that takes in the following input arguments:</p> <pre><code>lowest score: 0 in this example highest score: 100 in this example 'step' size: 10 in this example </code></pre> <p>As ever, if possible, I would like to write such a query using ANSI SQL. If I have to choose a database, then in order of DECREASING preference, it would have to be:</p> <ul> <li>PostgreSQL</li> <li>MySQL</li> </ul> <p>Could someone please explain how I may be able to write an SQL query that does this kind of grading, using the above table as an example?</p> <p><strong>[Edit]</strong></p> <pre><code>Sample input data 1, 'homer', 10.5 2. 'santas little helper', 15.2 3, 'bart', 20.5 4, 'marge', 40.5 5. 'lisa', 100 </code></pre> <p>I will have an SQL function grade_rank() - that ranks the student: </p> <p>The arguments for function grade_rank() are :</p> <pre><code>1st argument: LOWEST possible score value 2nd argument: HIGHEST possible score value 3rd argument: step size, which determines the levels/divisions between the ranks select id, name, grade_rank(0,100, 10) grade from student_scores; </code></pre> <p>the output (based on the input above) should be:</p> <pre><code>1, homer, GRADE9 2. santas liitle helper GRADE9 3, bart, GRADE8 4, marge, GRADE6 5. lisa, GENIUS </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload