0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

SQL group by句を使用して結果をまとめて表示する

Posted at

目的

  • SQLのgroup by句を使用して結果をある程度まとめて表示する方法をまとめる

情報

  • SQL句は小文字で記載する。

  • 下記SQLを順に実行してテーブルを作成した。

    create table items (category varchar(10), name varchar(255), price int);
    insert into items values ('stationery', 'eraser', 100);
    insert into items values ('stationery', 'pen', 150);
    insert into items values ('clothing', 't-shirt', 3000);
    insert into items values ('stationery', 'note', 100);
    insert into items values ('clothing', 'belt', 4000);
    insert into items values ('clothing', 'hat', 2000);
    insert into items values ('food', 'orange', 80);
    insert into items values ('food', 'apple', 120);
    insert into items values ('stationery', 'scissors', 500);
    insert into items values ('clothing', 'socks', 200);
    
  • 下記のような「items」テーブルが存在しているものとする。(下記情報はSQLshow full columns from items;を実行して確認)

    +----------+--------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    | Field    | Type         | Collation          | Null | Key | Default | Extra | Privileges                      | Comment |
    +----------+--------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    | category | varchar(10)  | utf8mb4_0900_ai_ci | YES  |     | NULL    |       | select,insert,update,references |         |
    | name     | varchar(255) | utf8mb4_0900_ai_ci | YES  |     | NULL    |       | select,insert,update,references |         |
    | price    | int          | NULL               | YES  |     | NULL    |       | select,insert,update,references |         |
    +----------+--------------+--------------------+------+-----+---------+-------+---------------------------------+---------+
    
  • 上記のテーブルに下記のようなデータが格納されているものとする。(下記情報はSQL'select * from items;'を実行して確認した)

    +------------+----------+-------+
    | category   | name     | price |
    +------------+----------+-------+
    | stationery | eraser   |   100 |
    | stationery | pen      |   150 |
    | clothing   | t-shirt  |  3000 |
    | stationery | note     |   100 |
    | clothing   | belt     |  4000 |
    | clothing   | hat      |  2000 |
    | food       | orange   |    80 |
    | food       | apple    |   120 |
    | stationery | scissors |   500 |
    | clothing   | socks    |   200 |
    +------------+----------+-------+
    

普通の足し算の出力

  1. まずは基本としてpriceカラムの値をすべて足して出力する。

  2. 下記SQLを実行することでpriceカラムの値をすべて足したものを出力する事ができる。

    select sum(price) from items;
    
  3. sum()句を使用することで指定したカラムのテーブル内のすべての値を足した値は出力することができる。しかしcategoryが一致しているもののみの合計金額を出力したいときは困る。

方法

  1. categoryカラムが一致しているレコードのpriceカラムの値の合計を出力する。

    select category, sum(price) from items group by category;
    
  2. 下記の内容が出力される。

    +------------+------------+
    | category   | sum(price) |
    +------------+------------+
    | stationery |        850 |
    | clothing   |       9200 |
    | food       |        200 |
    +------------+------------+
    
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?