Select total members and amount they paid - SQL
I need help for generating a SQL for MySQL database.
I have three tables:
Organisations
members
Payments
Organisations table:
+------------+---------+--------+
| id | name |website |
+------------+---------+--------+
| 1 | AAA | a.com |
|-------------------------------+
| 2 | BBB | b.com |
+------------+---------+--------+
Members table:
+------------+-------------------+--------+-----------------+-----------+
| id | organisation_id |name | Payment_confirm | join_date |
+------------+-------------------+--------+-----------------+-----------+
| 1 | 1 | james | 1 | 2013-8-02 |
|-----------------------------------------+-----------------+-----------+
| 2 | 1 | Jimmy | 0 | 2013-8-25 |
+------------+-------------------+--------+-----------------+-----------+
| 3 | 2 | Manny | 1 | 2013-07-02|
|-----------------------------------------+-----------------+-----------+
| 4 | 1 | Kim | 1 | 2013-09-02|
+------------+-------------------+--------+-----------------+-----------+
Payments table:
+------------+-------------------+--------+-----------------+----------------+
| 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
|
+------------+-------------------+--------+-----------------+----------------+
How can i select the following:
Expecting the following output:
+------------+-------------------+--------+-----------------+
| Org name | Revenue |untransferred amount |
+------------+-------------------+--------------------------+
| AAA | 150 | 0 |
|-----------------------------------------------------------+
| BBB | 200 | 200 |
+------------+-------------------+--------------------------+
Org name = organisation name
Revenue = Total amount received
untransferred amount = transferred_at is null (payments table)
Thanks in advanced.
No comments:
Post a Comment