Заменить нулевые значения в правом внешнем соединении на предыдущую непустую строку - PullRequest
5 голосов
/ 29 июня 2011

У меня есть этот запрос

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'

Он возвращает следующее

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

То, что я хочу, это когда материал имеет нулевое значение, замените его следующим ненулевым значением.В этом случае он заменил бы его на AD215XX

Поэтому вывод будет выглядеть как

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

Возможно ли это сделать?Может ли кто-нибудь помочь, пожалуйста.

Спасибо, Элай

Ответы [ 2 ]

1 голос
/ 29 июня 2011
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'
0 голосов
/ 30 июня 2011
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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...