目录
需求
实现
mysql
oracle
pg
hive
达梦
需求
在数据质量检核中,经常会要求检查数据数据符合某些要求,比较某个字段是否是数字或者是否是日期等等,经常这种就用到数据库正则
实现
参数说明:
通用规则,${PARAM}换成合适的正则表达式,${TABLE}换成实际数据表,${COL}换成实际字段
mysql
校验手机号
select (b - a) as c0, b as c1,(case when b=0 then 0 else round((b-a)*1.0/b,2) end) as c2 from (( select count(1) as a from ${TABLE} where ${COL} regexp '^((13[0-9])|(14(0|[5-7]|9))|(15([0-3]|[5-9]))|(16(2|[5-7]))|(17[0-8])|(18[0-9])|(19([0-3]|[5-9])))[0-9]{8}$' ) a, (select count(1) as b from ${TABLE}) b)
oracle
校验手机号
select (b - a) as c0, b as c1,(case when b=0 then 0 else round((b-a)*1.0/b,2) end) as c2 from ( select count(1) as a from ${TABLE} where regexp_like(${COL},'^((13[0-9])|(14(0|[5-7]|9))|(15([0-3]|[5-9]))|(16(2|[5-7]))|(17[0-8])|(18[0-9])|(19([0-3]|[5-9])))[0-9]{8}$')) a, (select count(1) as b from ${TABLE}) b
pg
select sum(case when cast(${COL} as varchar) ~${PARAM} then 0 else 1 end) as c0,count(*) as c1 from ${TABLE} where ${COL} is not null
hive
通用规则,${PARAM}换成合适的正则表达式,${TABLE}换成实际数据表,${COL}换成实际字段
select sum(case when cast(${COL} as varchar) regexp ${PARAM} then 0 else 1 end) as c0,count(*) as c1 from ${TABLE}
达梦
校验手机号
select nvl(b - a,0),nvl(a,0), nvl(b,0), decode(b,0,0,(b-a)*1.00/b) from ( select count(1) as a from ${TABLE} where REGEXP_LIKE(${COL}, '^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\d{8}$') ) a,( select count(1) as b from ${TABLE}) b