4

1対多の関係にある2つのテーブルを結合して、その集計結果を表示する方法はどうすればいいのでしょうか?

clientsとinvestmentsのテーブルがあり、それぞれに以下のデータを挿入しているとします。

create table clients (
  client_id int not null auto_increment primary key,
  email varchar(255)
);

create table investments (
  investment_id int not null auto_increment primary key,
  amount numeric(8,2),
  client_id int,
  foreign key (client_id) references clients(client_id)
);

mysql> select * from clients;
+-----------+-------------+
| client_id | email       |
+-----------+-------------+
|         1 | [email protected] |
|         2 | [email protected] |
|         3 | [email protected] |
|         4 | [email protected] |
|         5 | [email protected] |
+-----------+-------------+
5 rows in set (0.00 sec)

mysql> select * from investments;
+---------------+--------+-----------+
| investment_id | amount | client_id |
+---------------+--------+-----------+
|             1 | 101.00 |         1 |
|             2 | 102.00 |         1 |
|             3 | 103.00 |         1 |
|             4 | 201.00 |         2 |
|             5 | 202.00 |         2 |
|             6 | 301.00 |         3 |
+---------------+--------+-----------+

これらのテーブルを結合して、client毎にメールアドレスとinvestmentsの数、investmentsの合計金額を出したいと考えています。

mysql> select client_id, count(*), sum(amount) from investments group by client_id;
+-----------+----------+-------------+
| client_id | count(*) | sum(amount) |
+-----------+----------+-------------+
|         1 |        3 |      306.00 |
|         2 |        2 |      403.00 |
|         3 |        1 |      301.00 |
+-----------+----------+-------------+

この結果にメールアドレスを足すイメージです。

+-----------+----------+-------------+-------------+
| client_id | email    |    count(*) | sum(amount) |
+-----------+----------+-------------+-------------+

どうやってもうまくemailが入りません。どうすればいいのでしょうか?

3 件の回答 3

2

以下はサブクエリを使う場合です。

select client_id, 
       ( select email from clients where
           client_id = investments.client_id ) as email,
       count(*), sum(amount) from investments group by client_id;

+-----------+-------------+----------+-------------+
| client_id | email       | count(*) | sum(amount) |
+-----------+-------------+----------+-------------+
|         1 | [email protected] |        3 |      306.00 |
|         2 | [email protected] |        2 |      403.00 |
|         3 | [email protected] |        1 |      301.00 |
+-----------+-------------+----------+-------------+
1
  • ありがとうございます!できました。
    – coffee man
    2016年5月31日 13:36
1

window関数を使ったやり方です。
http://lets.postgresql.jp/documents/technical/window_functions

実行結果
http://sqlfiddle.com/#!15/9eecb7db59d16c80417c72d1e1f4fbf1/9304

WITH clients AS (
SELECT * FROM (
    VALUES
        (1, '[email protected]'),
        (2, '[email protected]'),
        (3, '[email protected]'),
        (4, '[email protected]'),
        (5, '[email protected]')
) AS clients(client_id, email)
),
investments AS (
SELECT * FROM (
    VALUES
        (1, 101.00, 1),
        (2, 102.00, 1),
        (3, 103.00, 1),
        (4, 201.00, 2),
        (5, 202.00, 2),
        (6, 301.00, 3)
) AS investments(investment_id, amount, client_id)
)
SELECT
    T.client_id,
    count(*) AS count,
    -- 集約される行の値はすべて同じなので、max/minなどの集約関数でよい
    max(T.sum_of_amount) AS sum_of_amount,
    max(T.email) AS email
FROM (
    SELECT
        T.client_id,
        sum(T.amount) over (partition by T.client_id) AS sum_of_amount,
        S.email
    FROM
        investments T
    LEFT JOIN
        clients S USING(client_id)
) T
GROUP BY
    T.client_id
;
1
  • はじめて知りました。勉強になります。ありがとうございます
    – coffee man
    2016年6月17日 2:04
0

結合してから潰すやり方です。

mysql> select client_id, email, count(*),sum(amount) from clients join investments using (client_id) group by client_id;
+-----------+-------------+----------+-------------+
| client_id | email       | count(*) | sum(amount) |
+-----------+-------------+----------+-------------+
|         1 | [email protected] |        3 |      306.00 |
|         2 | [email protected] |        2 |      403.00 |
|         3 | [email protected] |        1 |      301.00 |
+-----------+-------------+----------+-------------+
3 rows in set (0.00 sec)
1
  • ありがとうございます!できました。
    – coffee man
    2016年5月31日 13:36

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。