I’m looking for SQL query for a problem that I have.
I have 2 columns – the first is “employee full name” (containing FULL name) and the second is “Hours”.
Employee Full Name | Hours | grading
-------------------------------------
john ran 122
john medi 177
john mat 138
jack rom 66
jack ton 88
I would like to update the database with the “grading” column in the following way:
grouping employees by their FIRST name, and then grading them (in ascending order) by working hours (highest working hours for employee with a specific first name) gets grade of 1, second 2, etc…).
The result should be like this:
Employee Full Name | Hours | grading
------------------------------------
john ran 122 3
john medi 177 1
john mat 138 2
jack rom 66 2
jack ton 88 1
There are no 2 employees with the same full name
You can use a
CTEto perform this update usingrow_number()See SQL Fiddle with Demo
Ideally, you will want to normalize your database to have the first and last name in separate columns.