@@ -19,8 +19,9 @@
|
||||
- 在一维数组上做 TS-SQL 时,优先使用 `thisRow` 和 `thisRowIndex`。
|
||||
- `select` 返回二维结果,`sselect` 返回一维结果,`vselect` 返回单值,`mselect` 返回 `Matrix`。
|
||||
- `where`、`group by`、`order by` 可以直接接在 `from` 后面继续使用;`order by` 支持 `asc`/`desc` 与多列逗号分隔。
|
||||
- 分组后按聚集条件筛选用 `having`(`where` 不能用聚集);`having` 里用 `countof([字段])` 或 `countof(1)`,不要用 `countof(*)`。
|
||||
- 分组后按聚集条件筛选用 `having`(`where` 不能用聚集);计数可用 `countof([字段])`、`countof(1)`、`countof()`,也可用带空格的 `countof( * )`。无空格的 `countof(*)` 会与块注释起始符 `(*` 冲突。
|
||||
- 多表 `join` 时,字段访问应写成 `[表序号].["字段名"]`;`on` 可用 `and` 写多条件;`[表序号].*` 取整表列。
|
||||
- 多表联接中,`thisRow(表序号)` 和 `thisRowIndex(表序号)` 分别取得指定来源表的当前整行与原始下标。
|
||||
- 联接类型:`left join` 保留左表、`right join` 保留右表、`full join` 保留双方、`cross join` 笛卡尔积、逗号联接等价于 `cross join`;不匹配处用 `nil` 填充。
|
||||
- `select` 列表支持 `distinct` 去重、`as 别名`、`as nil`(参与计算但不返回)、`起始列 to 结束列` 字段区间、`selectopt(位选项)`、`drange(区间/M of N)`。
|
||||
- 聚集函数统一形态 `Func(Expr[, Cond[, N[, MovingFirst[, CacheId]]]])`:条件聚集、移动聚集、多字段聚集、`refof(Expr, N)` 引用相对行;`aggof('名', Expr)` 调用自定义聚集回调。
|
||||
@@ -233,6 +234,23 @@ writeLn(join_result[0]["V2"]);
|
||||
100
|
||||
```
|
||||
|
||||
联接上下文里的指定来源当前行:
|
||||
|
||||
代码块身份:可直接照写示例
|
||||
|
||||
```tsl
|
||||
left_rows := array(("id": 1, "v": 10), ("id": 2, "v": 20));
|
||||
right_rows := array(("id": 2, "v": 20), ("id": 3, "v": 30));
|
||||
join_rows := select thisRow(1) as "LeftRow",
|
||||
thisRow(2) as "RightRow",
|
||||
thisRowIndex(1) as "LeftIndex",
|
||||
thisRowIndex(2) as "RightIndex"
|
||||
from left_rows join right_rows on [1].["id"] = [2].["id"]
|
||||
end;
|
||||
```
|
||||
|
||||
结果说明:唯一匹配行中,`LeftRow["v"] = 20`、`RightRow["v"] = 20`、`LeftIndex = 1`、`RightIndex = 0`。
|
||||
|
||||
### `thisGroup`
|
||||
|
||||
<!-- section-id: syntax-13-011 -->
|
||||
@@ -773,6 +791,29 @@ writeLn(ref_prev[1]["Expr1"]);
|
||||
- 移动聚集:`avgof(表达式, 条件, N, MovingFirst)` 取当前行往前 N 条的滑动统计
|
||||
- `refof(表达式, N)` 引用前 N 行的值(`N` 为负则往后);首行无前值时返回 `0`
|
||||
|
||||
`*` 作为聚集输入时必须和左括号留空格,避免 `(*` 被词法层识别为块注释:
|
||||
|
||||
代码块身份:可直接照写示例
|
||||
|
||||
```tsl
|
||||
a := array(("x": 1, "y": 10), ("x": 3, "y": 20));
|
||||
row_count := vselect countof( * ) from a end;
|
||||
column_avg := select avgof( * ) from a end;
|
||||
writeLn(row_count);
|
||||
writeLn(column_avg[0]["Expr1"]);
|
||||
writeLn(column_avg[0]["Expr2"]);
|
||||
```
|
||||
|
||||
代码块身份:输出片段
|
||||
|
||||
```text
|
||||
2
|
||||
2.0
|
||||
15.0
|
||||
```
|
||||
|
||||
说明:`avgof( * )` 对每列分别聚集;这里两列平均值依次为 `2`、`15`。
|
||||
|
||||
### `group by ... having`
|
||||
|
||||
<!-- section-id: syntax-13-024 -->
|
||||
@@ -800,7 +841,7 @@ A
|
||||
说明:
|
||||
|
||||
- `having 聚集条件` 在分组后筛选(上例只保留成员数大于 1 的 `A` 组)
|
||||
- `having` 里的计数用 `countof([字段])` 或 `countof(1)`
|
||||
- `having` 里的计数优先用 `countof([字段])`、`countof(1)` 或 `countof()`;确需星号时写成 `countof( * )`
|
||||
|
||||
代码块身份:反例 / 不可照写
|
||||
|
||||
@@ -808,7 +849,7 @@ A
|
||||
having_rows := select ["cls"] from a group by ["cls"] having countof(*) > 1 end;
|
||||
```
|
||||
|
||||
`countof(*)` 这种带 `*` 的写法不成立,会报 `CountOf ( not found`。计数改用 `countof([字段])` 或 `countof(1)`。
|
||||
无空格的 `countof(*)` 会把 `(*` 词法组合解释成块注释开头,随后报 `CountOf ( not found`。改用 `countof( * )`、`countof()`、`countof([字段])` 或 `countof(1)`。
|
||||
|
||||
### `thisOrder` 与多列 `order by`
|
||||
|
||||
@@ -967,7 +1008,7 @@ query_result := select * from source_rows end;
|
||||
- 在 `left join` 时省略 `on` 子句或不用 `[表序号].["字段"]` 形式。
|
||||
- 在 `insert` 时漏掉 `insertfields` 或字段数与值数不匹配。
|
||||
- 期望 `update`/`delete` 返回新数组;它们直接修改原数组。
|
||||
- 用 `countof(*)` 数行数;`*` 星号形式不被支持,改用 `countof([字段])` 或 `countof(1)`。
|
||||
- 写无空格的 `countof(*)`,使 `(*` 与块注释起始符冲突;改用 `countof( * )`、`countof()`、`countof([字段])` 或 `countof(1)`。
|
||||
|
||||
代码块身份:反例 / 不可照写
|
||||
|
||||
@@ -975,7 +1016,7 @@ query_result := select * from source_rows end;
|
||||
n := vselect countof(*) from source_rows end;
|
||||
```
|
||||
|
||||
`countof(*)` 会报 `CountOf ( not found`。数行数改用 `countof([字段])` 或 `countof(1)`。
|
||||
`countof(*)` 会报 `CountOf ( not found`,原因是 `(*` 与块注释起始符冲突。星号写法加空格为 `countof( * )`,或改用 `countof()` / 明确表达式。
|
||||
|
||||
代码块身份:反例 / 不可照写
|
||||
|
||||
|
||||
Reference in New Issue
Block a user