简体   繁体   English

当 SQL 中有多个继续 Null 值时,如何根据前几个月的值填充 Null 值

[英]How do I populate the Null Values based on Previous months value, when there are multiple continues Null values in SQL

Below is the input table:下面是输入表:

Month Value价值
1 1个 200 200
2 2个 - -
3 3个 - -
4 4个 300 300
5 5个 - -

Expected Output:预计 Output:

Month Value价值
1 1个 200 200
2 2个 200 200
3 3个 200 200
4 4个 300 300
5 5个 300 300

I did try using LAG function in SQL, as a result I was able to populate value for the immediate NULL values which is Month 2 in above case but next month which is Month 3 was still having Null values我确实尝试在 SQL 中使用 LAG function,结果我能够填充即时 NULL 值的值,在上述情况下是第 2 个月,但下个月是第 3 个月仍然有 Null 值

You can build a query using the LEAD function to convert your data into ranges as the following:您可以使用LEAD function 构建查询,将数据转换为范围,如下所示:

from_month从_month to_month to_month Value价值
1 1个 3 3个 200 200
4 4个 5 5个 300 300

Then use the update join statement to update your table as the following:然后使用update join语句更新您的表,如下所示:

Update table_name Set Value = D.Value
From table_name T Join
(
  Select Month As from_month, 
       Lead(Month, 1, (select max(month) from table_name) +1)
       Over(Order By Month) -1 As to_month,
       Value
  From table_name 
  Where Value Is Not Null
) D
On T.month Between D.from_month And D.to_month
Where T.Value Is Null;

See demo看演示

You can emulate the LAST_VALUE function to ignore nulls by creating partitions with:您可以通过创建分区来模拟LAST_VALUE function 以忽略空值:

  • one non-null value一个非空值
  • following null values (excluding next non-null value)以下 null 个值(不包括下一个非空值)

then updating null values to the max for each partition.然后将 null 值更新为每个分区的最大值。

WITH cte AS (
    SELECT *, COUNT(Value) OVER(ORDER BY Month) AS partitions
    FROM tab
), cte2 AS (
    SELECT Month, MAX(Value) OVER(PARTITION BY partitions) AS Value 
    FROM cte
)
UPDATE tab
SET tab.Value = cte2.Value
FROM cte2
WHERE tab.Month = cte2.Month;

Check the demo here .此处查看演示。

You can do this with a simple correlation and an updatable table expression:您可以使用简单的关联和可更新的表表达式来做到这一点:

update u set value = v
from (
    select *, 
        (select top(1) value 
        from t t2 where t2.month < t.month 
            and t2.value is not null 
            order by month desc
        )v
    from t
    where t.value is null
)u;

If you don't like the old hat solutions you can do it in one pass too:如果您不喜欢旧帽子解决方案,您也可以一次性完成:

with testdata as (
    select *
    from    (
        values (1, 200), (2, 50), (3,NULL), (4,300), (5, NULL), (6, NULL), (7, 10)
        ) x (mon, val)
    )
select mon, val AS orig
,   cast(substring(maxvalue2, 31, 30) as int) AS new
FROM (
        SELECT  MAX(CAST(mon AS BINARY(30)) + CAST(val AS BINARY(30))) OVER(ORDER BY mon) AS maxValue2
        ,   *
        FROM    testdata
    ) x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据另一个表中的值填充一个表中的空值,其中连接基于子字符串 - How do I populate null values in one table based on values in another, where the join is based on a substring SQL-用先前的值填充NULL值 - SQL - Fill NULL values with previous value SQL将多行中的字段的空值填充为先前的非空值 - SQL fill null values for a field in multiple rows as previous non null value 如何用前一行的值填充 null 列? - How do I fill null columns with values from the previous row? Oracle SQL - 如何根据以前的非空记录计算空值 - Oracle SQL - How to calculate null values based on previous non-null record 如何获取空值的先前值 - How to get Previous Value for Null Values 如何为具有先前值的空值提供标签 - How to give label for null values with previous value 如何在SQL中排除具有&#39;0&#39;值的行而不排除具有NULL值的行? - How do I exclude the rows with '0' values but not the rows with NULL values in SQL? SQL - 如何使用 LEFT Join 填充先前匹配记录的 NULL 值而不是 NULL 值 - SQL - How to fill NULL values with NOT NULL value of previous matched record with LEFT Join 动态SQL查询可基于单元格中的空值忽略空值 - Dynamic SQL Query to ignore null values based on null value in cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM