【问题标题】:compare records within same table比较同一表中的记录
【发布时间】:2017-02-02 19:47:06
【问题描述】:

我有以下结构的 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 子句在子查询中不起作用。

附:我正在使用 sybase。

【问题讨论】:

    标签: sql sybase


    【解决方案1】:

    好的 - 更改为显然不能使用 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)
    

    【讨论】:

    • 我在子查询中使用,所以在 sybase TOP 中无法在子查询中工作。
    【解决方案2】:

    请试试这个

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

    【讨论】:

      猜你喜欢
      • 2016-08-15
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      相关资源
      最近更新 更多