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 の使用を開始

第6章. GROUP BYとORDER BY

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

データの集計(ロールアップ、サマライズ、グループ化とも呼ばれる)とは、多数のレコードから何らかの合計を作成することである。 合計、最小、最大、カウント、平均は一般的な集計演算子である。SQLでは、これらの合計を指定した列でグループ化することができ、集約の範囲を簡単に制御することができる。

レコードのグループ化

まず、最も単純な集計を実行する:テーブルのレコード数をカウントする。SQLエディタを開き、 station data のレコードのカウントを取得する:

SELECT COUNT(*) AS record_count FROM station_data;

COUNT(*) はレコードのカウントを意味する。WHERE のように、他のSQL演算子と組み合わせて使うこともできる。トルネードが存在したレコードの数をカウントするには、次のように入力する:

SELECT COUNT(*) AS record_count FROM station_data
WHERE tornado = 1;

竜巻が発生した3,000件の記録が確認された。 しかし、もし年ごとにカウントを分けたいとしたらどうだろうか(図6-1)。それもこのクエリでできる:

SELECT year, COUNT(*) AS record_count FROM station_data
WHERE tornado = 1
GROUP BY year;
Getting a tornado count by year
図6-1. 年別の竜巻カウントを取得する

このデータが急に意味を持つようになった。年別の竜巻目撃カウントである。このクエリを分解して、どうしてこのようなことが起こったのかを見てみよう。

まず、year を選択し、次にレコードからCOUNT(*) を選択し、tornado が真であるレコードだけをフィルタリングする:

SELECT year, COUNT(*) AS record_count FROM station_data
WHERE tornado = 1
GROUP BY year;

ただし、yearグループ化することも指定している。これによって、事実上、年ごとのレコード数をカウントすることができる。太字で強調表示された最後の行が、このグループ化を実行している:

SELECT year, COUNT(*) AS record_count FROM station_data
WHERE tornado = 1
GROUP BY year;

このデータを複数のフィールドでスライスすることができる。yearmonth によるカウントが欲しければ、month フィールドでもグループ化できる(図6-2):

SELECT year, month, COUNT(*) AS record_count FROM station_data
WHERE tornado = 1
GROUP BY year, month
図6-2. 年・月別竜巻カウント。

GROUP BYまた、 で列を指定する代わりに、序数位を使用することもできる。序数位 は、SELECT ステートメント内の各項目の数値位置に対応する。したがって、 ...

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