SAS SQL サブクエリ

【SAS】サブクエリについて解説

投稿日:2023年10月15日 更新日:

今回はSQLのfrom句にサブクエリを使用した場合の動きについて、解説します。

[from句]

まずは通常のSQLプロシージャから。


/* サブクエリ(from句_1) */
data data1;
  Id1=1111; Id2=1111; Id3=1111;
    output;
  Id1=2222; Id2=2222; Id3=2222;
    output;
  Id1=3333; Id2=3333; Id3=3333;
    output;
run;


proc sql;
  select *
  from data1;
quit;
  • 正常にSQLプロシージャが実行される。

/* サブクエリ(from句_2) */
data data1;
  Id1=1111; Id2=1111; Id3=1111;
    output;
  Id1=2222; Id2=2222; Id3=2222;
    output;
  Id1=3333; Id2=3333; Id3=3333;
    output;
run;


proc sql;
  select *
  from (
    select *
    from data1
  )
quit;
  • from句にSQL文を埋め込める。(サブクエリ)
  • 正常にSQLプロシージャが実行される。

/* サブクエリ(from句_3) */
data data1;
  Id1=1111; Id2=1111; Id3=1111;
    output;
  Id1=2222; Id2=2222; Id3=2222;
    output;
  Id1=3333; Id2=3333; Id3=3333;
    output;
run;


proc sql;
  select a1.*
  from (
    select *
    from data1
  ) a1;
quit;
  • from句にSQL文を埋め込める。(サブクエリ)
  • サブクエリの抽出結果により、作成したテーブルをa1としている。
  • 正常にSQLプロシージャが実行される。

[select句]

まずは通常のSQLプロシージャから。


/* サブクエリ(from句_1) */
data data1;
  Id1=1111; Id2=1111; Id3=1111;
    output;
  Id1=2222; Id2=2222; Id3=2222;
    output;
  Id1=3333; Id2=3333; Id3=3333;
    output;
run;


proc sql;
  select *
  from data1;
quit;
  • 正常にSQLプロシージャが実行される。

/* サブクエリ(select句_2) */
data data1;
  Id1=1111; Id2=1111; Id3=1111;
    output;
  Id1=2222; Id2=2222; Id3=2222;
    output;
  Id1=3333; Id2=3333; Id3=3333;
    output;
run;


proc sql;
  select 
    Id1
    ,Id2
    ,(
      select Id3
      from data1
    )
  from data1;
quit;
  • select句にSQL文を埋め込める。(サブクエリ)
  • data1テーブルからId3のみを切り出し、小テーブルとしてから、Id1・Id2とくっつける。
  • select句でサブクエリを使う時は、サブクエリは1行ずつ返さないとエラーとなる。(今回は3行返している)

/* サブクエリ(select句_3) */
data data1;
  Id1=1111; Id2=1111; Id3=1111;
    output;
  Id1=2222; Id2=2222; Id3=2222;
    output;
  Id1=3333; Id2=3333; Id3=3333;
    output;
run;


proc sql;
  select 
    Id1
    ,Id2
    ,(
      select Id3
      from data1
      where Id3 = 1111
    )
  from data1;
quit;
  • select句にSQL文を埋め込める。(サブクエリ)
  • data1テーブルからId3のみを切り出し、小テーブルとしてから、Id1・Id2とくっつける。
  • select句でサブクエリを使う時は、サブクエリは1行ずつ返さないとエラーとなる。(今回は1行ずつ返しているため、成功)

[where句]


/* サブクエリ(where句_1) */
data data1;
  Id1=1111; Id2=1111; Id3=1111;
    output;
  Id1=2222; Id2=2222; Id3=2222;
    output;
  Id1=3333; Id2=3333; Id3=3333;
    output;
run;


proc sql;
  select *
  from data1
  where Id1 = 1111;
quit;
  • 正常にSQLプロシージャが実行される。(where句を用いたSQL文)

/* サブクエリ(where句_2) */
data data1;
  Id1=1111; Id2=1111; Id3=1111;
    output;
  Id1=2222; Id2=2222; Id3=2222;
    output;
  Id1=3333; Id2=3333; Id3=3333;
    output;
run;


proc sql;
  select *
  from data1
  where Id1 = (
                select Id1
                from data1
                where Id1=1111
  );
quit;
  • where句にSQL文を埋め込める。(サブクエリ)
  • data1テーブルからId1=1111のみを切り出し、小テーブルとしてから、where条件として扱う。
  • where=句でサブクエリを使う時は、サブクエリは1行返さないとエラーとなる。(今回は1行返しているため、成功)

/* サブクエリ(where句_3) */
data data1;
  Id1=1111; Id2=1111; Id3=1111;
    output;
  Id1=2222; Id2=2222; Id3=2222;
    output;
  Id1=3333; Id2=3333; Id3=3333;
    output;
run;


proc sql;
  select *
  from data1
  where Id1 = (
                select Id1
                from data1
  );
quit;
  • where句にSQL文を埋め込める。(サブクエリ)
  • data1テーブルからId1のみを切り出し、小テーブルとしてから、where条件として扱う。
  • where=句でサブクエリを使う時は、サブクエリは1行返さないとエラーとなる。(今回は3行返しているため、エラー)

/* サブクエリ(where句_4) */
data data1;
  Id1=1111; Id2=1111; Id3=1111;
    output;
  Id1=2222; Id2=2222; Id3=2222;
    output;
  Id1=3333; Id2=3333; Id3=3333;
    output;
run;


proc sql;
  select *
  from data1
  where Id1 IN (
                select Id1
                from data1
  );
quit;
  • where句にSQL文を埋め込める。(サブクエリ)
  • data1テーブルからId1のみを切り出し、小テーブルとしてから、where条件として扱う。
  • where in句でサブクエリを使う時は、サブクエリは複数行返しても正常に動く。
  • where in(1111, 2222, 3333)みたいな感じ。

-SAS, SQL, サブクエリ

執筆者:


comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

関連記事

【SAS】PUTLOGステートメントは文字列をログに出力する。

今回はPUTLOGについて解説します。 /* putlog_1 */ data _null_; putlog ‘2022/03/26’; run; putlogにより、ログに文字列を出力できる。%pu …

【SAS】PATHNAME関数はライブラリのディレクトリパスを返す

今回はPATHNAME関数について解説します。 data data1; Id1 = pathname(“work”); run; pathnameによりライブラリworkのディレクトリパスを代入できる …

【SAS】NODUPKEYはソート時に重複を削除する

今回はNODUPKEYについて解説します。 data data1; id=”A”; chiku=”SHINJUKU1″; 数字=1; output; id=”A”; chiku=”SHINJUKU2″ …

【SAS】ROUNDは四捨五入して数値を丸める

今回はROUNDについて解説します。 /* 正常形 */ data work.data1; _100 = round(1234.56789, 100); _10 = round(1234.56789, …

【SAS】”nは本来使用できない文字を使用可能にする。その2 SQLのSELECT文

今回も”nについて解説します。 (前回:【SAS】”nは本来使用できない文字を使用可能にする。 | ビジネスイッチ (how-to-business.com)) (参考:Solved: what th …