DB2中一些常用sql函数

1.merge into ...using ...when matched then ...
        应用场景:此函数一般用于表与表之间字段的更新,判断B表和A表是否满足ON中条件,如果满足则用B表去更新A表,如果不满足,则将B表数据插入A表,是有有很多可选项。

       用例:有一个表T,有两个字段a、b,我们想在表T中做Insert/Update,如果条件满足,则更新T中b的值,否则在T中插入一条记录。
       merge into 目标表 t1 using 源表 t2  on(t1.条件字段1 = t2.条件字段1 and t1.条件字段2 = t2.条件字段2 ……)  when matched then update set t1.更新字段 = t2.字段 when  not macthed then insert into t1(字段1, 字段2 ……)values(值1, 值2 ……)

2.replace
       update [table_name] set [column_name] = replace([column_name], '被替换的数据', '替换的数据') 

3.case when ... end
     case when 条件 then 结果1 else 结果2 end 其中when可以重复多次
     示例: select case when 条件2 then 结果1 when 条件2 then 结果2 else 结果3 end from [table_name]

4.row_number() over()
      应用场景:此函数一般用于给每条数据添加序号
      用法:row_number() over(partition by 分组列 order by 排序列 desc)
      示例: select 字段1, row_number()over(partition by 分组列 order by 排序列 desc) rownum from [table_name]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值