Yesterday I went to an interview with one of these questions:
There is a score sheet with only two fields, names and grades. How do you query a student's name, score, and rank in a table with an SQL statement?
I can not think of a specific implementation, I have two ideas: one is to associate a query through the join of his ranking of the SQL statement, which is implemented through group by.
Answer to even feel a little guilty. How do you realize this?
Suppose: The table name is course, and two fields are named and score, respectively.
Implementation statement:
Select students, scores,
(select COUNT (*) from table WHERE A. Score <=) as rank from
table as a
Method Two:
Declare @Course table (name varchar, CJ Int)
INSERT into @Course
Select ' A ', the
union ALL
Select ' B ',
UNION ALL
Select ' C ', the Select
* FROM (select
1+count (name) from @Course where CJ>T.CJ) m C,NAME,CJ from
@Course t
) tem
where name= ' B '
If there is a record of the same score, Goodspeed's statement does have some flaws, but I made a slight change according to his statement is perfect.
Select Name,score,
(select COUNT (*) +1 from course WHERE a.score<score) as sort from course as an order by
sort
More please come here to view the comments