比较同一表中的记录

时间:2017-02-02 10:28:24

标签: sql sybase

我有Student_Table,结构如下。

 Student_Note  Table 
  student_id  seq_num  note 
  11212         1       firstnote
  11212         2       secondNote 
  11212         3       thirdNote
  21232         1       secondstudentnote1
  21232         2       secondstudentnote2

等等

我希望获得特定学生的最新笔记(最大的seq_num)。

我尝试过以下查询

  select tn.note from Student_Note tn   JOIN Student_Note tn1
            ON (tn.student_id =tn1.student_id AND  tn.seq_num < tn1.seq_num)
             where tn.student_id=11212

它提供了多行。如何实现上述方案?

我忘了提到我使用上面的查询作为子查询。根据sybase,TOP子句在子查询中不起作用。

P.S。我正在使用sybase。

2 个答案:

答案 0 :(得分:0)

好的 - 改为显然不能使用TOP .........

select tn.note from Student_Note tn   
where tn.student_id=11212
and tn.seq_num = (SELECT MAX(seq_num) from Student_Note WHERE Student_Note.Student_Id = tn.Student_Id)

答案 1 :(得分:0)

请试试这个

select      substring(max(str(seq_num,10,'0') + note),11,len(note))
from        Student_Note 
where       student_id=11212
相关问题