Would you please help me to find out whether I am on the right way? I am new to SQL, so there many things that are not obvious to me.
Let’s assume I have two tables.
The first is a list of subscribers:
subscribers
+--------+------------+
|subscID | name |
+--------+------------+
| 123 | SomeName00 |
| 456 | SomeName01 |
| 789 | SomeName02 |
| 012 | SomeName03 |
| 345 | SomeName04 |
+--------+------------+
And the second is a call log (or something like that), including the subscribers’ incomes and expenditures with transaction IDs and the current state of their accounts:
transactions
+--------+---------------------+--------+-----------+----------+
| trnID | date |subscID | amount | balance |
+--------+---------------------+-------+------------+----------+
| 321456 | 2012-03-13 11:10:00 | 456 | 70.0000 | 90.0000 |
| 234567 | 2012-03-16 15:05:00 | 456 | -45.0000 | 45.0000 |
| 345678 | 2012-03-19 17:27:00 | 456 | 15.0000 | 60.0000 |
| 654321 | 2012-04-22 17:34:00 | 456 | -10.0000 | 50.0000 |
| 543210 | 2012-04-15 15:45:00 | 789 | 20.0000 | 30.0000 |
| 567890 | 2012-05-16 13:30:00 | 789 | -10.0000 | 20.0000 |
| 876543 | 2012-02-29 11:00:00 | 012 | 20.0000 | 5.0000 |
| 678901 | 2012-03-31 09:40:00 | 012 | 10.0000 | 15.0000 |
| 456789 | 2012-03-31 21:09:00 | 012 | -13.0000 | 2.0000 |
| 432109 | 2012-02-23 14:01:00 | 345 | -30.0000 | 27.0000 |
| 012345 | 2012-03-24 19:57:00 | 345 | 40.0000 | 67.0000 |
| 765432 | 2012-03-27 13:28:00 | 345 | -14.0000 | 53.0000 |
+--------+---------------------+--------+-----------+----------+
Initially I had two tasks:
1. To count the number of transactions for every subscriber for
March 2012 (including those from the first table who are not in the
second table, like 123 SomeName00, and those who didn’t have any
transactions in March 2012, like 789 SomeName02).
2. To count the ending balance for every subscriber for March 2012
(again, including those from the first table who are not in the
second table and those who didn’t have any transactions in March
2012).
I’ve handled the first one this way:
SELECT name, COUNT(transactions.subscID) AS num_of_trns
FROM subscribers
LEFT JOIN transactions
ON subscribers.subscID = transactions.subscID
AND transactions.date LIKE "2012-03%"
GROUP BY subscribers.subscID
It seems to work, giving the following result:
+------------+------------+
| name |num_of_trns |
+------------+------------+
| SomeName00 | 0 |
| SomeName01 | 3 |
| SomeName02 | 0 |
| SomeName03 | 2 |
| SomeName04 | 2 |
+------------+------------+
Then I tried to reuse the code so that to solve the second task by changing COUNT(transactions.subscID) to transactions.balance and adding one more condition to LEFT JOIN (transactions.date = MAX(transactions.date)), like this:
SELECT name, transactions.balance AS trns_blnc
FROM subscribers
LEFT JOIN transactions
ON subscribers.subscID = transactions.subscID
AND transactions.date LIKE "2012-03%"
AND transactions.date = MAX(transactions.date) --this is incorrect
GROUP BY subscribers.subscID
But this approach turned out to be absolutely wrong (I suppose, MySQL just didn’t understand which values I was trying to compare).
Then I decided to make use of the first task result by once again left-joining another instance of transactions to it (sorry if I use the wrong terminology) on the condition that the instance’s date = the old table’s date:
SELECT march_trns.name, balance FROM (
SELECT name, date
FROM subscribers
LEFT JOIN transactions
ON subscribers.subscID = transactions.subscID
AND transactions.date LIKE "2012-03%"
GROUP BY subscribers.subscID
) AS march_trns
LEFT JOIN transactions AS transactions2
ON march_trns.date = transactions2.date
But quickly learned that march_trns.date and, as a result, balance was selected randomly (or I just failed to find out any pattern in its selection) among the values, meeting the LIKE "2012-03%" condition. Moreover, there were NULLs in my resulting table (I guess, it’s because I was no longer using COUNT that would count all the rows, incl. NULLs):
Have: Want:
+------------+----------+ +------------+----------+
| name | balance | | name | balance |
+------------+----------+ +------------+----------+
| SomeName00 | NULL | | SomeName00 | 0.0000 |
| SomeName01 | 90.0000 | | SomeName01 | 60.0000 |
| SomeName02 | NULL | | SomeName02 | 0.0000 |
| SomeName03 | 2.0000 | | SomeName03 | 2.0000 |
| SomeName04 | 67.0000 | | SomeName04 | 53.0000 |
+------------+----------+ +------------+----------+
So, I have two problems:
- I need to retrieve the end month balance values
- and to print out the NULL values as
0s.
I would be much obliged to you if you show me the right direction.
or by using
YEARandMONTHSOURCE