9.20 使用代码解析表中字段
使用代码解析序表中的字符型字段。
求 80 后的平均工资,年龄需要从证件号码中提取。员工表部分数据如下:
| ID | Name | Identification | Salary |
|---|---|---|---|
| 1 | Rebecca | Driving license:495319197411204628 | 7000 |
| 2 | Ashley | ID number:103263198007194980 | 11000 |
| 3 | Rachel | ID number:721125197012173641 | 9000 |
| 4 | Emily | ID number:619124198503071617 | 7000 |
| 5 | Ashley | ID number:248238197505138795 | 16000 |
| … | … | … | … |
单独的函数无法直接求解时,可以使用多个函数逐步解析处理字符串。
脚本:
| A | |
|---|---|
| 1 | =connect(“db”).query@x(“select * from Employee”) |
| 2 | =A1.run(Identification=Identification.regex(“\D*(\d+)”)(1)) |
| 3 | =A2.run(Identification=mid(Identification,7,4)) |
| 4 | =A3.run(Identification=number(Identification)) |
| 5 | =A4.select(Identification>=1980 && Identification <=1989) |
| 6 | =A5.avg(Salary) |
A1 连接数据源,读取员工表
A2 使用 S.regex() 函数读取身份证的数字部分
A3 使用 mid() 函数,读取身份证明的 7 到 10 位,即出生年份
A4 使用 number() 函数,将年份串解析为数字
A5 选出出生日期在 80 年代的员工
A6 计算平均工资
运行结果:
| ID |
|---|
| 7256.16 |
