Skip to Content
SQLクックブック 第2版 ―データベースエキスパート、データサイエンティストのための実践レシピ集
book

SQLクックブック 第2版 ―データベースエキスパート、データサイエンティストのための実践レシピ集

by Anthony Molinaro, Robert de Graaf, 嶋田 健志, 木下 哲也
February 2022
Intermediate to advanced
560 pages
8h 5m
Japanese
O'Reilly Japan, Inc.
Content preview from SQLクックブック 第2版 ―データベースエキスパート、データサイエンティストのための実践レシピ集

7章数値

本章では、数値計算などの数値に関する一般的な演算を取り上げます。一般に、複雑な計算を行う上で、SQLは第一候補とはなりませんが、日常的な数値演算には効果的です。さらに重要なことに、組織のデータはやはりSQLをサポートするデータベースやデータウェアハウスに置かれているのが最も一般的なので、SQLを使ってそのデータを調べて評価することは、そのデータを活用するのに必須です。この節の手法は、データサイエンティストが将来の分析に最も有望なデータを判断するするのにも役立つように選んでいます。

[Tip]

本章のレシピの中には、集約関数とGROUP BY句を利用するものがあります。グループ化のことをよく知らなければ、少なくとも付録Aの「A.1 グループ化」を読んでください。

レシピ7.1 平均値を計算する

問題

テーブル内のすべての行または行の一部に対して、列の平均値を計算したい。例えば、全従業員の平均給与と部署ごとの平均給与を知りたい。

解決策

全従業員の給与の平均値を計算するには、給与が含まれる列にAVG関数を適用するだけです。

WHERE句を付けなければ、すべての非NULLの平均値を計算します。

select avg(sal) as avg_sal
  from emp

   AVG_SAL
----------
2073.21429

部署ごとの平均給与を計算するには、GROUP BY句を使って各部署に対応するグループを作成します。

select deptno, avg(sal) as ...
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

詳解 Rustアトミック操作とロック ―並行処理実装のための低レイヤプログラミング

詳解 Rustアトミック操作とロック ―並行処理実装のための低レイヤプログラミング

Mara Bos, 中田 秀基
戦略的データサイエンス入門 ―ビジネスに活かすコンセプトとテクニック

戦略的データサイエンス入門 ―ビジネスに活かすコンセプトとテクニック

Foster Provost, Tom Fawcett, 竹田 正和, 古畠 敦, 瀬戸山 雅人, 大木 嘉人, 藤野 賢祐, 宗定 洋平, 西谷 雅史, 砂子 一徳, 市川 正和, 佐藤 正士

Publisher Resources

ISBN: 9784873119779Other