0

我有这种格式的表格数据

studno  name    level   year   term  subject1  subject2  subject3 
212    victor     l1    2000    1      45        56        80
213     HOM       l1    2000    1      42        56        70
214     ken       l1    2000    1      60        70        50
215    ted        l1    2000    1      46        36        47
212    victor     l1    2000    2      45        36        68
213    Hom        l1    2000    2      38        78        49
214    ken        l1    2000    2      38        34        62

我想要的输出如下

studno  name    level   year   term  subject1 sub1rank subject2  sub2rank 
213    victor     l1    2000    1      42        3       56        2 
214    HOM        l1    2000    1      60        1       70        1 
215    TED        l1    2000    1      46        2        36       3 
212   victor      l1    2000    2      45      2           36      1  
213    hOM        l1    2000    2      38      3           36      1  
214    KEN        l1    2000    2      38      3           32      3  
215    TED        l1    2000    2      90      1           30      4 

我已经设法获得排名,但问题是如何获得每年、级别、学期和学科的排名。另一个问题是,如果我使用嵌套语句并尝试在 mysql 数据库中创建视图,则会引发错误,“视图的 SELECT 包含 FROM 子句中的子查询”

4

1 回答 1

0

You can do this with correlated subqueries in the select clause. If I understand correctly, something like this:

select t.*,
       (select COUNT(distinct t1.subject1)
        from t t2
        where t2.level = t.level and t2.year = t.year and t2.term = t.term and
              t2.subject1 >= t.subject1
       ) as subj1rank,
       (select COUNT(distinct t2.subject2)
        from t t2
        where t2.level = t.level and t2.year = t.year and t2.term = t.term and
              t2.subject2 >= t.subject2
       ) as subj2rank
from t

The count(*) might be count(distinct subject1) (etc.), depending on how you treat ties.

于 2013-04-23T15:16:34.420 回答