12.8 外键映射的不存在性检测
在两个表中,根据外键映射的不存在性检测查找记录。
根据销售表和客户表,查询 2014 年每个新增客户的销售总额。
| Sales |
|---|
| ID |
| CustomerID |
| OrderDate |
| … |
| Customer |
|---|
| ID |
| Name |
| City |
| … |
使用 A.join() 函数的 @d 选项,只保留无匹配的记录。
脚本:
| A | |
|---|---|
| 1 | =connect(“db”) |
| 2 | =A1.query(“select * from Sales where year(OrderDate)=2014”) |
| 3 | =A1.query@x(“select * from Customer”) |
| 4 | =A2.join@d(CustomerID ,A3:ID) |
| 5 | =A4.groups(CustomerID; sum(Amount):Amount) |
A1 连接数据库
A2 查询 2014 年的销售记录
A3 查询客户表
A4 使用A.join@d ()从销售表中选出客户ID在客户表中不存在的记录
A5 分组汇总每个客户的销售额
运行结果:
| CustomerID | Amount |
|---|---|
| DOS | 11830.1 |
| HUN | 57317.39 |
| … | … |
