oracle 连接字段,如何在Oracle中连接字段?

如果行与行之间的唯一区别是空值与非空值,则可以在连接之前使用聚合:

-- CTE for sample data

with "#TEMP" (city, country, state, id) as (

select 'Great Falls', null, 'VA', 12345 from dual

union all select 'Great Falls', 'USA', 'VA', 12345 from dual

)

-- actual query

select max(city) as city, max(country) as country, max(state) as state,

max(city) ||', ' ||max(country)|| ', '||max(state) as concat_fields

from "#TEMP"

group by id;

CITY COU ST CONCAT_FIELDS

----------- --- -- --------------------

Great Falls USA VA Great Falls, USA, VA

如果ID的行具有不同的非空值,那么这显然不起作用:

with "#TEMP" (city, country, state, id) as (

select 'Great Falls', null, 'VA', 12345 from dual

union all select 'London', 'UK', null, 12345 from dual

)

select max(city) as city, max(country) as country, max(state) as state,

max(city) ||', ' ||max(country)|| ', '||max(state) as concat_fields

from "#TEMP";

CITY CO ST CONCAT_FIELDS

----------- -- -- -------------------

London UK VA London, UK, VA

但是,您需要某种方法来确定哪一行是正确的-例如,如果这代表客户地址的历史记录,并且您需要“最新”版本,则从序列或日期列中确定。接下来的诀窍是知道如何确定哪个是“最新的”。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值