如何获取空值的先前值

Sha*_*bal 9 sql t-sql sql-server sql-server-2012

我的表中有下面的数据.

   | Id  |  FeeModeId   |Name        |   Amount|
   ---------------------------------------------
   | 1   |  NULL        | NULL       |   20    |
   | 2   |  1           | Quarter-1  |   5000  |
   | 3   |  NULL        | NULL       |   2000  |    
   | 4   |  2           | Quarter-2  |   8000  |
   | 5   |  NULL        | NULL       |   5000  |
   | 6   |  NULL        | NULL       |   2000  |
   | 7   |  3           | Quarter-3  |   6000  |
   | 8   |  NULL        | NULL       |   4000  |
Run Code Online (Sandbox Code Playgroud)

如何编写这样的查询以获得低于输出...

   | Id  |  FeeModeId   |Name        |   Amount|
   ---------------------------------------------
   | 1   |  NULL        | NULL       |   20    |
   | 2   |  1           | Quarter-1  |   5000  |
   | 3   |  1           | Quarter-1  |   2000  |    
   | 4   |  2           | Quarter-2  |   8000  |
   | 5   |  2           | Quarter-2  |   5000  |
   | 6   |  2           | Quarter-2  |   2000  |
   | 7   |  3           | Quarter-3  |   6000  |
   | 8   |  3           | Quarter-3  |   4000  |
Run Code Online (Sandbox Code Playgroud)

Mik*_*son 14

由于您使用的是SQL Server 2012 ...这是一个使用它的版本.它可能比其他解决方案更快,但您必须在数据上进行测试.

sum() over()当列中有值并保持值的当前值时,将通过Id添加来执行已排序的运行总和.然后使用计算的运行总和对结果进行分区.由运行总和生成的每个"组"行所排序的第一个值具有您想要的值.1nullfirst_value() over()Id

select T.Id,
       first_value(T.FeeModeId) 
          over(partition by T.NF 
               order by T.Id 
               rows between unbounded preceding and current row) as FeeModeId,
       first_value(T.Name)      
          over(partition by T.NS 
               order by T.Id 
               rows between unbounded preceding and current row) as Name,
       T.Amount
from (
     select Id,
            FeeModeId,
            Name,
            Amount,
            sum(case when FeeModeId is null then 0 else 1 end) 
              over(order by Id) as NF,
            sum(case when Name is null then 0 else 1 end) 
              over(order by Id) as NS
     from YourTable
     ) as T
Run Code Online (Sandbox Code Playgroud)

SQL小提琴

在SQL Server 2012之前可以运行的东西:

select T1.Id,
       T3.FeeModeId,
       T2.Name,
       T1.Amount
from YourTable as T1
  outer apply (select top(1) Name
               from YourTable as T2
               where T1.Id >= T2.Id and
                     T2.Name is not null
               order by T2.Id desc) as T2
  outer apply (select top(1) FeeModeId
               from YourTable as T3
               where T1.Id >= T3.Id and
                     T3.FeeModeId is not null
               order by T3.Id desc) as T3
Run Code Online (Sandbox Code Playgroud)

SQL小提琴


Tec*_*hDo 5

请尝试:

select 
    a.ID,
    ISNULL(a.FeeModeId, x.FeeModeId) FeeModeId,
    ISNULL(a.Name, x.Name) Name,
    a.Amount
from tbl a
outer apply
(select top 1 FeeModeId, Name 
    from tbl b 
    where b.ID<a.ID and 
        b.Amount is not null and 
        b.FeeModeId is not null and 
        a.FeeModeId is null order by ID desc)x
Run Code Online (Sandbox Code Playgroud)

或者

select 
    ID,
    ISNULL(FeeModeId, bFeeModeId) FeeModeId,
    ISNULL(Name, bName) Name,
    Amount
From(
    select 
        a.ID , a.FeeModeId, a.Name, a.Amount, 
        b.ID bID, b.FeeModeId bFeeModeId, b.Name bName,
        MAX(b.FeeModeId) over (partition by a.ID) mx
    from tbl a left join tbl b on b.ID<a.ID
    and b.FeeModeId is not null
)x 
where bFeeModeId=mx or mx is null
Run Code Online (Sandbox Code Playgroud)