0

我需要从前一行中取值,直到它达到另一个新值。LAG 不起作用,因为它只填充 1 行,我可能有 1 到 12 行,具体取决于订单号。

ColumnA是我的行号,columnB是我需要填充的列,直到它到达下一个非空白。非常感谢任何帮助,因为我没有想法。

行会是这样的

Columna  Columb  ColumnC  ColumnD
12       Yarn    Yellow   Coarse
13               Yellow   Coarse
14       Needles Size G   Steel
15               Size H   Wood
16               Size Y   Wood
17       Hooks   Fish     Steel
18               Whale    Steel
18               Dolphin  Steel
19               Mermaid  Steel
4

1 回答 1

0

给定字段 A 是一个不间断的序列..

Table
A        B        C        D
12       Yarn     Yellow   Coarse 
13                Yellow   Coarse 
14       Needles  Size G   Steel 
15                Size H   Wood 
16                Size Y   Wood 
17       Hooks    Fish     Steel 
18                Whale    Steel 
19                Dolphin  Steel 
20                Mermaid  Steel

我第一次通过它

Select 
   T1.A
   COALESCE(T2.A,T1.B) as B,
   T1.C,
   T1.D 

From
    Table as T1
    LEFT JOIN Table as T2 ON T2.A = T1.A -1

新答案

Select 
   T1.A
   Coalesce(
       T1.B,
       (
           Select top 1 B 
           FROM Table 
           WHERE A < T1.A 
            and B IS Not Null
           ORDER BY A DESC
       )
    ) as B,
    T1.C,
    T1.D 
FROM    
    Table as T1
于 2013-10-25T16:04:44.583 回答