选择他们支付的总成员和金额 - SQL

时间:2013-09-19 07:36:37

标签: mysql sql

我需要帮助为MySQL数据库生成SQL。

我有三张桌子:

  • 组织
  • 成员
  • 付款

组织表:

+------------+---------+--------+
|    id      |  name   |website |
+------------+---------+--------+
| 1          | AAA     | a.com  |
|-------------------------------+
| 2          | BBB     | b.com  |
+------------+---------+--------+

会员表:

+------------+-------------------+--------+-----------------+-----------+
|    id      |  organisation_id  |name    | Payment_confirm | join_date |
+------------+-------------------+--------+-----------------+-----------+
| 1          | 1                 | james  | 1               | 2013-8-02 |
|-----------------------------------------+-----------------+-----------+
| 2          | 1                 | Jimmy  | 0               | 2013-6-25 |
+------------+-------------------+--------+-----------------+-----------+
| 3          | 2                 | Manny  | 1               | 2013-07-02|
|-----------------------------------------+-----------------+-----------+
| 4          | 1                 | Kim    | 1               | 2013-09-02|
+------------+-------------------+--------+-----------------+-----------+

付款表

+------------+-------------------+--------+-----------------+----------------+
|    id      |  member_id        |amount  | transaction_id  | transferred_at |
+------------+-------------------+--------+-----------------+----------------+
| 1          | 1                 | 100    | T1001           | 2013-8-03      |
|-----------------------------------------+-----------------+--------------- +
| 2          | 2                 | 0      | null            | Null           |
+------------+-------------------+--------+-----------------+----------------+
| 3          | 3                 | 200    | T1002           | Null           |
|-----------------------------------------+-----------------+----------------+
| 4          | 4                 | 50     | T1005           | 2013-09-05     |
+------------+-------------------+--------+-----------------+----------------+

如何选择以下内容:

期待以下输出:

+------------+-------------------+--------+-----------------+---------------+--------------+ 
|  Org name  |  Revenue          |untransferred amount      | Total members | last 30 days |
+------------+-------------------+--------------------------+---------------+--------------+
| AAA        | 150               | 0                        | 3             |  2           |
|-----------------------------------------------------------+---------------+--------------+
| BBB        | 200               | 200                      | 1             |  0           |   
+------------+-------------------+--------------------------+---------------+--------------+


Org name              = organisation name
Revenue               = Total amount received
untransferred amount  = transferred_at is null (payments table)
Total members         = total members joined till today
last 30 days          = total members joined last 30 days 

先谢谢。

2 个答案:

答案 0 :(得分:4)

您需要加入表格,对结果进行分组并选择所需的逻辑:

SELECT   org.name,
         SUM(pmt.amount) AS revenue,
         SUM(IF(pmt.transferred_at IS NULL, pmt.amount, 0)) AS untransferred
FROM     Organisations org
    JOIN Members       mem ON mem.organisation_id = org.id
    JOIN Payments      pmt ON pmt.member_id       = mem.id
GROUP BY org.id

sqlfiddle上查看。

答案 1 :(得分:0)

select o.name, 
sum(amount) as Revenue,
sum(if(transferred_at is null, amount, 0)) as untransfered_ammt,
sum(if(join_date>=curdate() - interval 30 day, 1, 0)) as last_30_d


from organisations o
inner join members m on o.id=m.organisation_id
inner join payments p on p.member_id=m.member_id
group by 1