简体   繁体   English

比较同一表中的记录

[英]compare records within same table

I have Student_Table with following structure. 我有具有以下结构的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

so on 依此类推

I want to get latest note (which has largest seq_num) for a particilar student. 我想为有兴趣的学生获得最新笔记(最大seq_num)。

I have tried following query 我尝试了以下查询

  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

it is giving more than one row. 它提供了不止一排。 How to achieve aforementioned scenario ? 如何实现上述方案?

I forgot mention that I am using above query as subquery . 我忘了提到我将上述查询用作子查询。 As per sybase TOP clause will not work in subquery. 根据sybase,TOP子句在子查询中将不起作用。

PS I am using sybase. PS我正在使用sybase。

OK - changed as apparently cannot use TOP......... OK-更改为显然无法使用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)

Please try this 请尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM