问题描述
嗨,
有没有一种简单的方法可以在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中实现以下输出?
想要用国家名称填充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进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
【纯干货】Oracle 19C RU 19.27 发布,如何快速升级和安装?
Lucifer三思而后行
803次阅读
2025-04-18 14:18:38
Oracle数据库一键巡检并生成HTML结果,免费脚本速来下载!
陈举超
604次阅读
2025-04-20 10:07:02
【ORACLE】你以为的真的是你以为的么?--ORA-38104: Columns referenced in the ON Clause cannot be updated
DarkAthena
552次阅读
2025-04-22 00:13:51
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
541次阅读
2025-04-17 17:02:24
【ORACLE】记录一些ORACLE的merge into语句的BUG
DarkAthena
512次阅读
2025-04-22 00:20:37
一页概览:Oracle GoldenGate
甲骨文云技术
506次阅读
2025-04-30 12:17:56
火焰图--分析复杂SQL执行计划的利器
听见风的声音
476次阅读
2025-04-17 09:30:30
OR+DBLINK的关联SQL优化思路
布衣
387次阅读
2025-05-05 19:28:36
Oracle数据库Hint大全,31个使用案例,速来下载!
陈举超
371次阅读
2025-04-16 21:25:19
Oracle19C低版本一天遭遇两BUG(ORA-04031/ORA-00600)
潇湘秦
337次阅读
2025-04-16 17:05:16