1

我有一个问题,如果你能提供帮助,我将不胜感激。

在下图中,我想建立一个名为:最近分数的新列,如您所见,最近分数是用户上次获得的分数。如果我们按日期对行进行排序,则最近的得分是下一行的得分值。

在此处输入图像描述

我写了以下代码,但它是错误的:(

  SELECT [user-name],
         [submissions],
         [date],
         [score]

         (SELECT [score]
                 FROM [top-concept6] tc6
                 WHERE tc6.[user-name]=[top-concept6].[user-name]
                 AND tc6.[date]= (SELECT  TOP(1) [date] 
                 FROM [top-concept6] tcc6  
                 WHERE [date]<[top-concept6].[date] ORDER BY [date] DESC)) AS [recent-score]

   FROM [top-concept6]

这是 sql 小提琴:http ://sqlfiddle.com/#!3/74e3f

4

2 回答 2

1

请试试:

select 
    *,
    (select top 1 b.score from [top-concept6] b where b.[user-name]=a.[user-name] and b.date<a.date order by b.date desc) [Recent Score]
from 
    [top-concept6] a
于 2013-04-19T06:04:25.173 回答
1
select *, 
(select top 1 score from [top-concept6]
 where [user-name]=t1.[user-name]
       and [date]<t1.[date]
 order by date desc    
) recent_score
from [top-concept6] t1

SQLFiddel 演示

于 2013-04-19T06:05:27.273 回答