7.22 大数据表与大维表的关联查询
两个大数据表与一个有序的集文件相互关联查询。
根据相互关联的订单表、订单明细表和产品表,查询 2014 年 1 月各种产品的销售总量。其中订单表与订单明细表为大数据文件,产品表为对 ID 有序的集文件。

使用 cs.joinx() 函数连接文件,集文件对于连接字段必须有序。
脚本:
| A | |
|---|---|
| 1 | =file(“Detail.btx”).cursor() |
| 2 | =file(“Order.btx”).cursor().select(year(Date)==2014 && month(Date)==1) |
| 3 | =file(“Product.btx”) |
| 4 | =A1.joinx@i(ID,A2:ID) |
| 5 | =A4.joinx(ProductID,A3:ID,Name:ProductName) |
| 6 | =A5.groups(ProductName; count(~):Count) |
A1 创建订单明细表游标
A2 创建 2014 年 1 月的订单游标
A3 创建产品表集文件对象
A4 使用 cs.joinx 函数的 @i 选项进行连接过滤
A5 使用 cs.joinx 函数按产品 ID 连接订单明细表和产品表
A6 分组汇总各产品的销售数量
运行结果:
| ProductName | Count |
|---|---|
| Milk | 32 |
| Coffee | 60 |
| … | … |
