Skip to content

Commit ff292f7

Browse files
KKouldChasen-Zhang
andauthored
docs: add docs for 'ASOF JOIN' (#2520)
* docs: add docs for 'ASOF JOIN' * Update 02-join.md --------- Co-authored-by: z <[email protected]>
1 parent 445de96 commit ff292f7

File tree

1 file changed

+105
-1
lines changed

1 file changed

+105
-1
lines changed

docs/en/guides/54-query/02-join.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ Table "gift": This table lists the gift options for the VIP clients.
4545
| Coffee |
4646
| Soda |
4747

48+
Table trades: This table records trade transactions for various stock symbols.
49+
50+
| Symbol | Time | Price |
51+
| ------ |--------|--------|
52+
| AAPL | 100003 | 101 |
53+
| AAPL | 100007 | 103 |
54+
| MSFT | 100002 | 99 |
55+
| TSLA | 100010 | 200 |
56+
57+
58+
Table quotes: This table provides quote snapshots (bid/ask) for symbols at various timestamps.
59+
60+
| Symbol | Time | Bid | Ask |
61+
| ------ |--------| --- | --- |
62+
| AAPL | 100000 | 99 | 102 |
63+
| AAPL | 100005 | 100 | 104 |
64+
| MSFT | 100001 | 98 | 101 |
65+
| NVDA | 100006 | 150 | 155 |
66+
67+
4868
## Inner Join
4969

5070
The *inner join* returns the rows that meet the join conditions in the result set.
@@ -411,4 +431,88 @@ Output:
411431
```sql
412432
|100|Croissant|2000
413433
|106|Soda|4000
414-
```
434+
```
435+
436+
## Asof Join
437+
438+
An Asof Join (Approximate Sort-Merge Join) returns rows from the left table matched with the most recent row in the
439+
right table whose timestamp is less than or equal to the left's. It is commonly used in time series data to attach the
440+
latest contextual information, such as quotes, status, or sensor readings.
441+
442+
Unlike typical equi-joins, the join condition is based on inequality (usually `<=`) and may optionally include additional
443+
equality conditions.
444+
445+
### Syntax
446+
447+
```sql
448+
SELECT select_list
449+
FROM table_a ASOF
450+
JOIN table_b
451+
ON table_a.time >= table_b.time
452+
[ AND table_a.key = table_b.key]
453+
```
454+
455+
### Examples
456+
457+
The following example joins a trade record with the latest quote before or at the trade time for the same symbol:
458+
459+
```sql
460+
SELECT *
461+
FROM trades
462+
ASOF JOIN quotes
463+
ON trades.symbol = quotes.symbol
464+
AND trades.time >= quotes.time;
465+
```
466+
467+
For the definitions of the tables in the example, see [Example Tables](#example-tables).
468+
469+
Output:
470+
471+
```sql
472+
│ AAPL │ 100003101 │ AAPL │ 10000099102
473+
│ AAPL │ 100007103 │ AAPL │ 100005100104
474+
│ MSFT │ 10000299 │ MSFT │ 10000198101
475+
476+
```
477+
478+
The following example performs an ASOF LEFT JOIN, returning all trade records along with the latest quote before or at the trade time for the same symbol, if such a quote exists. If no matching quote exists, the quote fields will be NULL.
479+
480+
```sql
481+
SELECT *
482+
FROM trades
483+
ASOF LEFT JOIN quotes
484+
ON trades.symbol = quotes.symbol
485+
AND trades.time >= quotes.time;
486+
```
487+
488+
For the definitions of the tables in the example, see [Example Tables](#example-tables).
489+
490+
Output:
491+
492+
```sql
493+
│ AAPL │ 100003101 │ AAPL │ 10000099102
494+
│ MSFT │ 10000299 │ MSFT │ 10000198101
495+
│ AAPL │ 100007103 │ AAPL │ 100005100104
496+
│ TSLA │ 100010200NULLNULLNULLNULL
497+
```
498+
499+
The following example performs an ASOF RIGHT JOIN, returning all quote records along with the latest trade after or at the quote time for the same symbol, if such a trade exists. If no matching trade exists, the trade fields will be NULL.
500+
501+
```sql
502+
SELECT *
503+
FROM trades
504+
ASOF RIGHT JOIN quotes
505+
ON trades.symbol = quotes.symbol
506+
AND trades.time >= quotes.time;
507+
```
508+
509+
For the definitions of the tables in the example, see [Example Tables](#example-tables).
510+
511+
Output:
512+
513+
```sql
514+
│ AAPL │ 100003101 │ AAPL │ 10000099102
515+
│ AAPL │ 100007103 │ AAPL │ 100005100104
516+
│ MSFT │ 10000299 │ MSFT │ 10000198101
517+
NULLNULLNULL │ NVDA │ 100006150155
518+
```

0 commit comments

Comments
 (0)