7.6 关联查询:全连接
两个相互关联的表,通过全连接跨表计算。
根据相互关联的销售表和产品表,比较 2014 年每个月的产品销售情况。

使用 join() 函数的 @f 选项进行全连接。
脚本:
| A | |
|---|---|
| 1 | =connect(“db”) |
| 2 | =A1.query(“select ProductID, month(Date) as Month from Sales where year(Date)=2014”) |
| 3 | =A1.query@x(“select * from Product”) |
| 4 | =A2.switch(ProductID ,A3:ID) |
| 5 | =A4.group(Month) |
| 6 | =A5.(~.group@1(ProductID).new(ProductID.Name:Product, count(~):Count)) |
| 7 | =A6.(“A6(”+string(#)+“):”+string(#)+“,Product”).concat(“;”) |
| 8 | =join@f(${A7}) |
A1 连接数据库
A2 查询 2014 年的产品销售记录
A3 查询客户表
A4 使用 switch 函数将产品 ID 字段转换为对应记录
A5 按月份分组并排序
A6 每个月对产品去重,保留产品名称字段
A7 拼 join@f() 函数的参数串
A8 使用 join@f() 函数对 12 个月的数据进行全连接
运行结果:
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| (null) | [Cheese,3] | (null) | (null) | (null) | (null) | (null) | (null) | (null) | [Cheese,6] | (null) | (null) |
| (null) | [Coffee,7] | (null) | [Coffee,6] | [Coffee,9] | (null) | [Coffee,9] | (null) | (null) | (null) | (null) | [Coffee,8] |
| [Milk,3] | (null) | [Milk,5] | [Milk,7] | (null) | [Milk,6] | [Milk,8] | [Milk,3] | (null) | [Milk,6] | [Milk,4] | (null) |
| … | … | … | … | … | … | … | … | … | … | … | … |
