简体   繁体   English

将右外连接中的 null 值替换为先前的非 null 匹配行

[英]Replace null values in right outer join with a previous not null matched row

I have this query我有这个查询

select a.WeekNumber
        ,a.filedate
        ,a.customer
        ,material
        ,Quantity
from zfmon zf right outer join zfmonTemp a
on zf.customer = a.customer
        and zf.filedate = a.filedate
        and zf.material =  'AD215BY'

It returns the following它返回以下内容

WeekNumber FileDate                 Customer Material Quantity
1          2010-03-19 00:00:00.000  1008777  NULL     NULL
2          2010-03-12 00:00:00.000  1008777  AD215XX  3

What I want is for when the material is null replace it with the next not null value.我想要的是当材料为 null 时,将其替换为下一个不是 null 值。 In this case it would replace it with AD215XX在这种情况下,它将用 AD215XX 替换它

Therefore the output will look like因此 output 看起来像

WeekNumber FileDate                 Customer Material Quantity
1          2010-03-19 00:00:00.000  1008777  AD215XX  NULL
2          2010-03-12 00:00:00.000  1008777  AD215XX  3

Is that possible to do?那有可能吗? Can any one help please.任何人都可以帮忙吗?

Thanks, Eli谢谢,伊莱

select a.WeekNumber
        ,a.filedate
        ,a.customer
        ,isnull(material, (select top 1 material from zfmonTemp where weeknumber > zf.weeknumber and material is not null order by weeknumber)) material
        ,Quantity
from zfmon zf right outer join zfmonTemp a
on zf.customer = a.customer
        and zf.filedate = a.filedate
        and zf.material =  'AD215BY'
set @material = 'AD215BY';
select a.WeekNumber
        ,a.filedate
        ,a.customer
        ,coalesce(zf.material, @material) as Material
        ,zf.Quantity
from zfmon zf right outer join zfmonTemp a
on zf.customer = a.customer
        and zf.filedate = a.filedate
        and zf.material = @material

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM