填写缺失的数据

时间:2013-08-07 19:15:30

标签: sql sql-server tsql

我在SQL Server中工作。假设我有下表:

3/1/2013    aaa   7     5
1/1/2013    bbb   1     2
10/10/2012  ccc   5     8
9/9/2012    ddd   6     9
8/8/2012    bbb   2     3

我想从“bbb”行获取值并向上传播它们(按日期排序)基本上是:

3/1/2013    aaa   1     2
1/1/2013    bbb   1     2
10/10/2012  ccc   2     3
9/9/2012    ddd   2     3
8/8/2012    bbb   2     3

在SQL Server中执行此操作的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

非常基本的东西可能是

SELECT MT.Date, MT.Text, 
       CASE WHEN MT.Text = 'bbb' THEN Number
            ELSE (SELECT TOP 1 Number 
                               FROM MyTable MT2 
                               WHERE MT2.Date < MT.Date AND 
                                     MT2.Text = 'bbb'
                               ORDER BY MT2.Date DESC)
            END Number,
       CASE WHEN MT.Text = 'bbb' THEN Number2
            ELSE (SELECT TOP 1 Number2 
                               FROM MyTable MT2 
                               WHERE MT2.Date < MT.Date AND 
                                     MT2.Text = 'bbb'
                               ORDER BY MT2.Date DESC)
            END Number2 
       FROM MyTable MT

SQLFiddle:http://sqlfiddle.com/#!3/cbee5/3

或使用OUTER APPLY(它应该更快)

SELECT MT.Date, MT.Text, 
       CASE WHEN MT.Text = 'bbb' THEN MT.Number
            ELSE MT2.Number 
            END Number,
       CASE WHEN MT.Text = 'bbb' THEN MT.Number2
            ELSE MT2.Number2
            END Number2
       FROM MyTable MT
       OUTER APPLY (SELECT TOP 1 MT2.Number, MT2.Number2 
                                 FROM MyTable MT2
                                 WHERE MT.Text <> 'bbb' AND 
                                       MT2.Text = 'bbb' AND 
                                       MT2.Date < MT.Date
                                 ORDER BY MT2.Date DESC
                   ) MT2

SQLFiddle:http://sqlfiddle.com/#!3/cbee5/7

答案 1 :(得分:1)

要选择,您可以这么简单;

SELECT t1.date, t1.tag, t2.val, t2.val2
FROM Table1 t1
JOIN Table1 t2 
  ON t2.tag='bbb' AND t1.date >= t2.date
LEFT JOIN Table1 t3 ON t3.tag = 'bbb' AND t1.date >= t3.date AND t3.date > t2.date
WHERE t3.date IS NULL

An SQLfiddle to test with

更新表的内容,您可以使用几乎相同的查询;

UPDATE t1
SET val=t2.val, val2=t2.val2
FROM Table1 t1
JOIN Table1 t2 
  ON t2.tag='bbb' AND t1.date >= t2.date
LEFT JOIN Table1 t3 ON t3.tag = 'bbb' AND t1.date >= t3.date AND t3.date > t2.date
WHERE t3.date IS NULL

Another SQLfiddle