-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
235 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
+++ | ||
author = "Noboru Saito" | ||
title = "trdsql CROSS JOIN" | ||
date = "2024-04-18" | ||
description = "trdsql CROSS JOIN" | ||
weight = 28 | ||
tags = [ | ||
"trdsql", | ||
"CROSS", | ||
"JOIN", | ||
] | ||
categories = [ | ||
"trdsql", | ||
] | ||
+++ | ||
|
||
`CROSS JOIN` is an easy way to create a total. | ||
|
||
a.csv | ||
|
||
```CSV | ||
aa | ||
ab | ||
ac | ||
``` | ||
|
||
b.csv | ||
|
||
```CSV | ||
ba | ||
bb | ||
bc | ||
``` | ||
|
||
When you `CROSS JOIN` two CSV files, a 3x3 output is possible for all combinations. | ||
|
||
```console | ||
trdsql "SELECT * FROM a.csv CROSS JOIN b.csv" | ||
aa,ba | ||
aa,bb | ||
aa,bc | ||
ab,ba | ||
ab,bb | ||
ab,bc | ||
ac,ba | ||
ac,bb | ||
ac,bc | ||
``` | ||
|
||
You can also self-join a single file. | ||
For example, let's create a home and away total table. | ||
|
||
cleague.csv | ||
|
||
```CSV | ||
team | ||
Giants | ||
DeNA | ||
Tigers | ||
Carp | ||
Dragons | ||
Swallows | ||
``` | ||
|
||
To simply `CROSS JOIN`, it looks like this (you can't write JOIN conditions because there are none). | ||
|
||
```console | ||
trdsql -ih \ | ||
"SELECT h.team,a.team "\ | ||
" FROM cleague.csv AS h "\ | ||
" CROSS JOIN cleague.csv AS a" | ||
``` | ||
|
||
You can't play against your own team, so exclude the same team with `WHERE h.team != a.team`. | ||
|
||
```console | ||
trdsql -ih -omd \ | ||
"SELECT h.team AS home,a.team AS aware " \ | ||
" FROM cleague.csv AS h CROSS JOIN cleague.csv AS a "\ | ||
" WHERE h.team != a.team " | ||
``` | ||
|
||
| home | aware | | ||
|----------|----------| | ||
| Giants | DeNA | | ||
| Giants | Tirgers | | ||
| Giants | Carp | | ||
| Giants | Dragons | | ||
| Giants | Swallows | | ||
| DeNA | Giants | | ||
| DeNA | Tirgers | | ||
| DeNA | Carp | | ||
| DeNA | Dragons | | ||
| DeNA | Swallows | | ||
| Tirgers | Giants | | ||
| Tirgers | DeNA | | ||
| Tirgers | Carp | | ||
| Tirgers | Dragons | | ||
| Tirgers | Swallows | | ||
| Carp | Giants | | ||
| Carp | DeNA | | ||
| Carp | Tirgers | | ||
| Carp | Dragons | | ||
| Carp | Swallows | | ||
| Dragons | Giants | | ||
| Dragons | DeNA | | ||
| Dragons | Tirgers | | ||
| Dragons | Carp | | ||
| Dragons | Swallows | | ||
| Swallows | Giants | | ||
| Swallows | DeNA | | ||
| Swallows | Tirgers | | ||
| Swallows | Carp | | ||
| Swallows | Dragons | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
+++ | ||
author = "Noboru Saito" | ||
title = "trdsql generate_series" | ||
date = "2024-04-18" | ||
description = "trdsql generate_series" | ||
weight = 29 | ||
tags = [ | ||
"trdsql", | ||
"generate_series", | ||
"PostgreSQL", | ||
] | ||
categories = [ | ||
"trdsql", | ||
] | ||
+++ | ||
|
||
## generate_series | ||
|
||
PostgreSQL has a convenient function called `generate_series()`. | ||
This function works similarly to the Unix `seq` command. `generate_series()` also has an extension that can be used with timestamp types. | ||
|
||
The usage is simple. Specify the "start value", "end value", and "increment value (optional)" and execute. | ||
|
||
```console | ||
trdsql -driver postgres -dsn "dbname=trdsql_test" "SELECT * FROM generate_series(1,10)" | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
8 | ||
9 | ||
10 | ||
``` | ||
|
||
`generate_series()` is a function that returns a table and can be used instead of a table. | ||
(en: You can also write `SELECT generate_series(1,10)`). | ||
|
||
Of course, trdsql can easily incorporate input from external sources, so you can also use the `seq` command as a substitute. | ||
|
||
```console | ||
seq 1 10|trdsql "SELECT * FROM -" | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 | ||
7 | ||
8 | ||
9 | ||
10 | ||
``` | ||
|
||
The order of arguments for the `seq` command is "start value", "increment value (optional)", "end value". | ||
|
||
## Timestamp | ||
|
||
`generate_series()` can handle timestamps, so it's a bit tricky to output the 2020 calendar in Japanese, but it looks like this. | ||
|
||
```console | ||
trdsql -driver postgres -dsn "dbname=trdsql_test" \ | ||
"SET LC_TIME='C'; " \ | ||
"SELECT day " \ | ||
" FROM generate_series('2024-01-1'::timestamp,'2024-12-31','1 day') as day" | ||
2024-01-01T00:00:00Z | ||
2024-01-02T00:00:00Z | ||
2024-01-03T00:00:00Z | ||
2024-01-04T00:00:00Z | ||
.... | ||
2024-12-29T00:00:00Z | ||
2024-12-30T00:00:00Z | ||
2024-12-31T00:00:00Z | ||
``` | ||
|
||
## Increase the amount of data | ||
|
||
There are times when you want a certain amount of dummy data. If you want completely distributed random data, you need to use a dedicated tool. | ||
However, if you just want to increase the number of existing data, you can create it by CROSS JOINing `generate_series()` or the `seq` command. | ||
|
||
```console | ||
trdsql -driver postgres -dsn "dbname=trdsql_test" -ih -oh \ | ||
"SELECT ROW_NUMBER() OVER() AS id, name " \ | ||
" FROM header.csv CROSS JOIN generate_series(1,3) AS s" | ||
id,name | ||
1,Orange | ||
2,Melon | ||
3,Apple | ||
4,Orange | ||
5,Melon | ||
6,Apple | ||
7,Orange | ||
8,Melon | ||
9,Apple | ||
``` | ||
|
||
Using the seq command, it looks like this. | ||
|
||
(When processing a file with a header with -ih, the first line of `seq` is interpreted as a header, so starting from 0 will result in an extra line being output). | ||
|
||
```console | ||
seq 0 3|trdsql -driver sqlite3 -ih -oh \ | ||
"SELECT ROW_NUMBER() OVER() AS id, name " \ | ||
" FROM - CROSS JOIN header.csv" | ||
id,name | ||
1,Orange | ||
2,Melon | ||
3,Apple | ||
4,Orange | ||
5,Melon | ||
6,Apple | ||
7,Orange | ||
8,Melon | ||
9,Apple | ||
``` |