diff --git a/content/trdsql/24_graph.en.md b/content/trdsql/24_graph.en.md index 8e5e96489..7e43e8dd1 100644 --- a/content/trdsql/24_graph.en.md +++ b/content/trdsql/24_graph.en.md @@ -1,7 +1,7 @@ +++ author = "Noboru Saito" title = "trdsql graph" -date = "2024-04-19" +date = "2024-04-18" description = "trdsql graph creation method." weight = 24 tags = [ diff --git a/content/trdsql/25_library.en.md b/content/trdsql/25_library.en.md index c9c6f0ac5..b26baa65b 100644 --- a/content/trdsql/25_library.en.md +++ b/content/trdsql/25_library.en.md @@ -1,7 +1,7 @@ +++ author = "Noboru Saito" title = "trdsql library" -date = "2024-04-19" +date = "2024-04-18" description = "trdsql library usage." weight = 25 tags = [ diff --git a/content/trdsql/26_file_sql.en.md b/content/trdsql/26_file_sql.en.md index 26cd35e49..1d0f98f99 100644 --- a/content/trdsql/26_file_sql.en.md +++ b/content/trdsql/26_file_sql.en.md @@ -1,7 +1,7 @@ +++ author = "Noboru Saito" title = "trdsql SQL file" -date = "2024-04-19" +date = "2024-04-18" description = "trdsql SQL file" weight = 26 tags = [ diff --git a/content/trdsql/27_config.en.md b/content/trdsql/27_config.en.md index b4c0b3bff..48c02da8b 100644 --- a/content/trdsql/27_config.en.md +++ b/content/trdsql/27_config.en.md @@ -1,7 +1,7 @@ +++ author = "Noboru Saito" title = "trdsql config" -date = "2024-04-19" +date = "2024-04-18" description = "trdsql config" weight = 27 tags = [ diff --git a/content/trdsql/28_cross_join.en.md b/content/trdsql/28_cross_join.en.md new file mode 100644 index 000000000..2e45774b6 --- /dev/null +++ b/content/trdsql/28_cross_join.en.md @@ -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 | diff --git a/content/trdsql/29_generate_series.en.md b/content/trdsql/29_generate_series.en.md new file mode 100644 index 000000000..8208b23fd --- /dev/null +++ b/content/trdsql/29_generate_series.en.md @@ -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 +```