べすとえふぉーと

プログラミング等のノート 

SQL

INNER JOINでサブテーブルの最大レコードをくっ付ける

SQL

メインテーブルに関連付くサブテーブルのうち サブのIDが最大の情報を取ってくる場合のSQL select a.*,c.* from main_table a inner join ( select main_id,max(sub_id) sub_id from sub_table group by main_id ) b on a.main_id = b.main_id inner join su…

Update文内でCaseを使う

SQL

update_col1 = case when tbl1.datecolumn <= '20210101' then tbl1.value1 else tbl1.value2 end

結合条件内でcaseを使う

SQL

検証はSQLiteでやっているので他の環境でも使えるかとテーブル作成 CREATE TABLE "subtable" ( "date1" TEXT, "date2" TEXT, "date3" TEXT, "flg" TEXT ); CREATE TABLE "maintable" ( "dateval" TEXT, "value" TEXT );データ投入 insert into subtable ('da…

sqliteで結果をファイル出力

SQL

.outputでファイル名指定 .output output_file.txt select some_text from some_table;

SQLite3 でテーブル一覧取得

SQL

.tables

MacにPostgreSQLをソースからインストール

configureでパスを指定してインストール(足りないものがあれば適宜brew) 他のUNIXならだいたい同じかと ./configure --prefix=/Users/user/Desktop/postgresinstall make make install initdbを実行してからpostgresを起動 cd /Users/user/Desktop/postgre…