暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 向下复制行值,直到下一个非空行

ASKTOM 2019-11-06
668

问题描述

嗨,

有没有一种简单的方法可以在SQL中实现以下输出?

想要用国家名称填充null值,直到有了新的国家名称。

谢谢

数据为
(
从双重联盟中选择 “阿富汗阿富汗” 为country_currency,NULL为金额,“阿富汗” 国家
选择 'Kabul' 作为country_currency,'200' 作为金额,空国家从双联盟全部
选择 “其他地方” 作为国家/地区 _ 货币,选择 “100” 作为金额,从双联盟全部选择 “空国家/地区”
选择 “阿尔巴尼亚 (阿尔巴尼亚列克 (e))” 作为国家货币,空为金额,“阿尔巴尼亚” 国家从双重联盟全部
选择 “地拉那” 作为国家货币,“100” 作为金额,空国家从双联盟全部
选择 “shkodra” 为country_currency,“ 200 ”为“ 金额 ”,从双联盟全部选择“ 空国家”
选择 “vlore” 作为国家货币,“ 200 ”作为金额,空国家从双联盟全部
选择 “其他地方” 作为国家/地区 _ 货币,选择 “200” 作为金额,从双联盟全部选择 “空国家/地区”
选择 “阿尔及利亚 (阿尔及利亚第纳尔)” 作为国家货币,空为金额,“阿尔及利亚” 国家从双重联盟全部
选择 “阿尔及尔” 作为country_currency,选择 “100” 作为金额,从双联盟全部选择 “空国家”
选择 'Adrar' 作为country_currency,'200' 作为金额,空国家从双联合全部
选择 “oran” 为country_currency,“200” 为 “金额”,从双联盟全部选择 “空国家”
选择 “其他地方” 作为country_currency,选择 “200” 作为金额,从dual选择 “空国家”
)
选择
*
来自
数据

专家解答

谢谢你的测试数据。我们需要 * 定义 * 顺序的东西,所以我添加了一个序列号。在那之后,它很容易有一点滞后

SQL> with data as
  2  (
  3  select 1 x, 'Afghanistan Afghani' as country_currency, NULL as amount, 'Afghanistan' country from dual union all
  4  select 2,'Kabul' as country_currency, '200' as amount, NULL country from dual union all
  5  select 3,'Elsewhere' as country_currency, '100' as amount, NULL country from dual union all
  6  select 4,'Albania (Albania Lek(e))' as country_currency, null as amount, 'Albania' country from dual union all
  7  select 5,'Tirana' as country_currency, '100' as amount, NULL country from dual union all
  8  select 6,'Shkodra' as country_currency, '200' as amount, NULL country from dual union all
  9  select 7,'Vlore' as country_currency, '200' as amount, NULL country from dual union all
 10  select 8,'Elsewhere' as country_currency, '200' as amount, NULL country from dual union all
 11  select 9,'Algeria (Algerian Dinar)' as country_currency, null as amount, 'Algeria' country from dual union all
 12  select 10,'Algiers' as country_currency, '100' as amount, NULL country from dual union all
 13  select 11,'Adrar' as country_currency, '200' as amount, NULL country from dual union all
 14  select 12,'Oran' as country_currency, '200' as amount, NULL country from dual union all
 15  select 13,'Elsewhere' as country_currency, '200' as amount, NULL country from dual
 16  )
 17  select d.*,
 18         nvl(country, lag(country ignore nulls) over ( order by x ) ) as filler
 19  from data d
 20  order by x;

         X COUNTRY_CURRENCY         AMO COUNTRY     FILLER
---------- ------------------------ --- ----------- -----------
         1 Afghanistan Afghani          Afghanistan Afghanistan
         2 Kabul                    200             Afghanistan
         3 Elsewhere                100             Afghanistan
         4 Albania (Albania Lek(e))     Albania     Albania
         5 Tirana                   100             Albania
         6 Shkodra                  200             Albania
         7 Vlore                    200             Albania
         8 Elsewhere                200             Albania
         9 Algeria (Algerian Dinar)     Algeria     Algeria
        10 Algiers                  100             Algeria
        11 Adrar                    200             Algeria
        12 Oran                     200             Algeria
        13 Elsewhere                200             Algeria

13 rows selected.
复制


文章转载自ASKTOM,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论