Skip to content

Commit

Permalink
Add english version of 16_join.md
Browse files Browse the repository at this point in the history
  • Loading branch information
noborus committed Apr 11, 2024
1 parent d3cbdad commit 00e85fd
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 169 deletions.
167 changes: 0 additions & 167 deletions content/trdsql/15_import.md

This file was deleted.

86 changes: 86 additions & 0 deletions content/trdsql/16_join.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
+++
author = "Noboru Saito"
title = "trdsql JOIN"
date = "2024-04-11"
description = "trdsql JOIN multiple files"
weight = 16
tags = [
"trdsql",
"SQL",
"JOIN",
]
categories = [
"trdsql",
]
+++

JOIN multiple files with trdsql

Suppose you have the following two CSV files:

abc.csv

```CSV
1,AAA
2,BBB
3,CCC
```

price.csv

```CSV
1,100
2,500
3,50
```

The following is the result of joining the two files:

```CSV
1,AAA,100
2,BBB,500
3,CCC,50
```

With trdsql, you can write SQL JOINs directly by using file names instead of tables.

```console
trdsql "SELECT a.c1, a.c2, p.c2" \
"FROM abc.csv AS a" \
"LEFT JOIN price.csv AS p" \
"USING (c1)"
```

For a 1-to-1 JOIN with the same number of corresponding columns, the result is the same as `INNER JOIN`.
In the case of a LEFT JOIN, all rows of the specified abc.csv are displayed first, and price.csv is displayed only if there is a corresponding row.
This time, since there is no header in the CSV, the column names are common c1, c2, etc., so the leftmost column (c1) is used as the common column for `USING`. This is the same as `ON a.c1 = p.c1`.

When joining multiple CSV files, it is necessary to unify the presence or absence of headers.

However, if the extension is automatically identifiable, it is possible to mix CSV and LTSV.

unit.ltsv

```LTSV
id:1 unit:個
id:2 unit:箱
```

Join the result of the previous CSV JOIN with LTSV.

```console
trdsql -oat \
"SELECT a.c1, a.c2, p.c2, unit" \
" FROM abc.csv AS a" \
"LEFT JOIN price.csv AS p" \
"USING (c1)" \
"LEFT JOIN unit.ltsv AS u " \
"ON (a.c1 = u.id)"
+----+-----+-----+------+
| c1 | c2 | c2 | unit |
+----+-----+-----+------+
| 1 | AAA | 100 | 個 |
| 2 | BBB | 500 | 箱 |
| 3 | CCC | 50 | |
+----+-----+-----+------+
```
7 changes: 5 additions & 2 deletions content/trdsql/16_join.md → content/trdsql/16_join.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ categories = [
以下の2つのCSVファイルがあったとして、

abc.csv

```CSV
1,AAA
2,BBB
3,CCC
```

price.csv

```CSV
1,100
2,500
Expand All @@ -42,7 +44,7 @@ price.csv

trdsqlではテーブルの代わりにファイル名を使用すれば、そのままSQLのJOINが書けます。

```sh
```console
trdsql "SELECT a.c1, a.c2, p.c2" \
"FROM abc.csv AS a" \
"LEFT JOIN price.csv AS p" \
Expand All @@ -58,14 +60,15 @@ LEFT JOINの場合は、先に指定したabc.csvの行はすべて表示され
しかしながら、自動判別可能な拡張子になっていれば、CSVとLTSV等の混在は可能です。

unit.ltsv

```LTSV
id:1 unit:個
id:2 unit:箱
```

先程のCSVのJOINの結果に更にLTSVをJOINします。

```sh
```console
trdsql -oat \
"SELECT a.c1, a.c2, p.c2, unit" \
" FROM abc.csv AS a" \
Expand Down

0 comments on commit 00e85fd

Please sign in to comment.