Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL。我正在寻找一种方法,用重复的值填充数字之间的值_Sql - Fatal编程技术网

SQL。我正在寻找一种方法,用重复的值填充数字之间的值

SQL。我正在寻找一种方法,用重复的值填充数字之间的值,sql,Sql,顺序订单成本 1 NULL 2 NULL 3 NULL 4 NULL 5 NULL 6 NULL 7 5.766 8 NULL 9 5.767 1 5.766 2 5.766 3 5.766 4 5.766 5 5.766 6 5.766 7 5.766 8 5.767 9 5.767 我正在使用SQLServer2008R2。我正在寻找一种方法,当成本不存在时,复制较低顺序订单的值。

顺序订单成本

1    NULL
2    NULL
3    NULL
4    NULL
5    NULL
6    NULL
7    5.766
8    NULL
9    5.767
1    5.766
2    5.766
3    5.766
4    5.766
5    5.766
6    5.766
7    5.766
8    5.767
9    5.767
我正在使用SQLServer2008R2。我正在寻找一种方法,当成本不存在时,复制较低顺序订单的值。我试着从这个例子中学习,但失败了。例如,我想将上表转换为类似于以下内容的内容:

顺序订单成本

1    NULL
2    NULL
3    NULL
4    NULL
5    NULL
6    NULL
7    5.766
8    NULL
9    5.767
1    5.766
2    5.766
3    5.766
4    5.766
5    5.766
6    5.766
7    5.766
8    5.767
9    5.767

如果数字始终在增加,则可以使用ANSI min累积窗口函数:

select t.*, min(cost) over (order by SequentialOrder desc)
from t;
编辑:

因为问题没有用数据库标记,所以最通用的解决方案是关联子查询。它看起来像这样:

select t.*,
       (select t2.cost
        from t t2
        where t2.cost is not null and t2.SequentialOrder >= t.SequentialOrder
        order by t2.SequentialOrder desc
        fetch first 1 row only
       ) as cost_notnull
from t;
select sequentialOrder, max(cost) over (partition by cost) as ubound 
from <orders_table>
order by sequentialOrder

请注意,有些数据库使用TOP或LIMIT甚至其他方法,而不是ANSI标准的FETCH FIRST 1 ROW ONLY。

您可以尝试以下方法:

select t.*,
       (select t2.cost
        from t t2
        where t2.cost is not null and t2.SequentialOrder >= t.SequentialOrder
        order by t2.SequentialOrder desc
        fetch first 1 row only
       ) as cost_notnull
from t;
select sequentialOrder, max(cost) over (partition by cost) as ubound 
from <orders_table>
order by sequentialOrder
在任何情况下,您都必须考虑使用聚合函数进行分区和排序。

您可以使用外部应用程序来回填值。对于较大的数据集,可能需要限制要回填的距离,因为此类查询往往非常缓慢

SELECT SequentialOrder,
       COALESCE(o.Cost, details.Cost) AS Cost 
FROM   dbo.data o
       OUTER APPLY ( SELECT TOP 1 Cost
                     FROM     dbo.data i
                     WHERE    Cost IS NOT NULL
                     AND i.SequentialOrder> o.SequentialOrder
                     ORDER BY  SequentialOrder ASC
                ) details

对于给定id-搜索具有非空值的第一个最近记录并获取其值:

declare @t table(id int, val decimal(10, 3))

insert into @t values(1, NULL)
insert into @t values(2, NULL)
insert into @t values(3, NULL)
insert into @t values(4, NULL)
insert into @t values(5, NULL)
insert into @t values(6, NULL)
insert into @t values(7, 5.766)
insert into @t values(8, NULL)
insert into @t values(9, 5.767)

select 
    t1.id,
    (select top 1 t2.val 
        from @t as t2 
        where t2.id >= t1.id and t2.val is not null
        order by t2.id) as val
from @t as t1 

如果我们知道你在用什么数据库肯定会有帮助。。有些分区函数可以做这类事情。欢迎使用堆栈溢出。只是想让您知道,如果您在本网站上提问时提供具体细节,我们将不胜感激,正如@MikeChristensen在上文中所建议的。不幸的是,事实并非如此,而且时间间隔不同