需求:表中有三列: mobile1,mobile2,mobile3 ,三列电话只,如果mobile1为空,使用mobile2,如果mobile2 也为空,使用mobile3,
select coalesce(mobile1,replace(mobile2,' ',''),mobile3) mobile ,mobile1,mobile2,mobile3 from ods_customer_idv_dx t
在mobile2中使用replace 就是预付此列中有空格,所以先过滤掉.
这个coalesce函数其实相当于
select case when mobile1 is not null then mobile1 else
case when mobile2 is not null then mobile2
else mobile3 end
end mobile,mobile1,mobile2,mobile3 from ods_customer_idv_dx t
结果:
MOBILE MOBILE1 MOBILE2 MOBILE3
1 18060198050 18060198050
2 13906735859 13906735859
3 13930615515 13930615515
4 13806989550 13806989550
5 13563437579 13563437579
6 13561465566 13561465566
7 13629942078 13629942078
8 13897880287 13897880287
9 13873832566 13873832566
10 13997779138 13997779138
11 15873009090 15873009090
12 15092059251 15092059251
13 13961216690 13961216690
14 13806663263 13806663263
15 13587942738 13587942738
16 13933311898 13933311898
17 13906737622 13906737622
18 13518383255 13518383255
19 13673124759 13673124759
20 15532375676 15532375676
21 13176625599 13176625599
22 057164254726 057164254726