Skip to Content
SQL の使用を開始
book

SQL の使用を開始

by Thomas Nield
May 2025
Intermediate to advanced
134 pages
1h 43m
Japanese
O'Reilly Media, Inc.
Content preview from SQL の使用を開始

第7章. CASE文

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

SQLの真の定義であるJOIN 演算子を学ぶ準備はほぼ整った。しかしその前に、の章では、CASE と呼ばれる非常に便利な演算子について説明する。この演算子を使うと、1つ以上の条件付きで列の値を別の値と入れ替えることができる。

CASEステートメント

CASE ステートメントでは、1つ以上の条件と各条件に対応する値をマッピングすることができる。CASE 文はCASE という言葉で始め、END で締めくくる。これらのキーワードの間に、各条件を指定する。 WHEN [condition] THEN [value]で各条件を指定する。 [condition]と対応する [value]は自分で指定する。条件と値のペアを指定した後、どの条件も満たされなかった場合にデフォルトとなるキャッチオール値を指定することができ、これはELSE で指定される。 例えば、wind_speedwind_severity のカテゴリーに分類することができる(図7-1)。ここで、40を超える速度は'HIGH' 、30から40は'MODERATE' 、それ以下は'LOW' となる:

SELECT report_code, year, month, day, wind_speed,

CASE
    WHEN wind_speed >= 40 THEN 'HIGH'
    WHEN wind_speed >= 30 AND wind_speed < 40 THEN 'MODERATE'
    ELSE 'LOW'
END as wind_severity

FROM station_data
Categorizing wind severity into HIGH, MODERATE, and LOW
図7-1. 風の強さをHIGH、MODERATE、LOWに分類する。

実際には、AND wind_speed < 40 の条件付きは省略できる。マシンはCASE 文を上から下へと読み取り、最初に真と発見された条件付きが使用される(そして、それ以降の条件評価は停止される)。したがって、wind_speed が 43 のレコードがあれば、'HIGH' として評価されることは確実である。30より大きいが、'MODERATE' は代入されない。これを知ることで、少し効率的なクエリを作成することができる:

SELECT report_code, year, month, day, wind_speed,

CASE
    WHEN wind_speed >= 40 THEN 'HIGH'
    WHEN wind_speed >= 30 THEN 'MODERATE'
    ELSE 'LOW'
END as wind_severity

FROM station_data

CASE文をグループ化する

CASE ステートメントを作成し、それらをグループ化すると、非常に強力な変換を行うことができる。集計する前に1つ以上の条件付きで値を変換することで、興味深い方法でデータをスライスする可能性がさらに広がる。先ほどの例をさらに詳しく説明すると、yearwind_severity をグループ化し、それぞれのレコードのカウントを取得することができる。(また、GROUP BY を序数で使用し、 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

MySQL クックブック、第 4 版

MySQL クックブック、第 4 版

Sveta Smirnova, Alkin Tezuysal
コンピュータビジョンのための実践機械学習 ―モデルアーキテクチャからMLOpsまで

コンピュータビジョンのための実践機械学習 ―モデルアーキテクチャからMLOpsまで

Valliappa Lakshmanan, Martin Görner, Ryan Gillard, 大山 匠, 松田 晃一

Publisher Resources

ISBN: 9798341651029