0

我正在尝试在同一查询中获取学生的先前分数和先前的考试日期。我当前的查询工作正常,直到先前的分数正好落在 28 天的差异上。问题是当分数落在 29,30 或 31 天时。这是我当前的查询:

SELECT 
a.STUDENTID, 
b.CURRENTSCORE,
b.CURRENTSCOREDT, 
    (select CURRENTSCORE 
      from SCORES 
      where STUDENT = a.STUDENTID
        and ENDDTE=trunc(a.DATECOL)-28 
        and COURSE=b.COURSE and rownum =1
       ) as PRIORSCORE, 
trunc(b.ENDDTE)-28 as PRIORCOREDT,
b.COURSE
FROM 
  TABLE1 a,
  SCORES b
WHERE
 TRUNC(a.DATECOL) = (SELECT MAX(TRUNC(ENDDTE)) 
                       FROM SCORES
                     ) 
and a.STUDENTID = b.STUDENT 
and b.COURSE='Course1';

如何使这变得更加动态,以便无论何时参加考试/测试,我都能获得先前的分数。

4

1 回答 1

-1

我认为类似的东西可能会起作用:

with rankedscores as (
(select
        scores.*, 
        rank() over (partition by studentid order by datecol desc) daterank 
from
        scores
where
        COURSE='Course1' 
)
select
        a.STUDENTID,      
        b.CURRENTSCORE,   
        b.CURRENTSCOREDT, 
        c.CURRENTSCORE priorscore,
        c.CURRENTSCOREDT priorscoredt
from
        table1 a,
        rankedscores b,
        rankedscores c
where
        a.STUDENTID = b.student(+) and
        a.STUDENTID = c.student(+) and
        nvl(b.daterank,1) = 1 and
        nvl(c.daterank,2) = 2   

(未测试...)

于 2013-11-22T20:26:25.297 回答