Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:55:01+00:00 2026-06-14T22:55:01+00:00

Would you please help me to find out whether I am on the right

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:55:02+00:00Added an answer on June 14, 2026 at 10:55 pm
    SELECT  name, 
            COALESCE(COUNT(transactions.subscID), 0) AS num_of_trns,
            COALESCE(d.balance,0)
    FROM    subscribers
            LEFT JOIN transactions
                ON subscribers.subscID = transactions.subscID 
                    AND transactions.date LIKE "2012-03%"
            LEFT JOIN
            (
                SELECT  a.subscID, a.balance
                FROM    transactions a
                        INNER JOIN
                        (
                            SELECT subscID, MAX(date) maxDate
                            FROM transactions
                            WHERE transactions.date LIKE "2012-03%"
                            GROUP BY subscID
                        ) b ON a.subscID = b.subscID AND
                                a.date = b.maxDate
    
            ) d ON subscribers.subscID = d.subscID
    GROUP BY subscribers.subscID
    ORDER BY Name
    
    • SQLFiddle Demo

    or by using YEAR and MONTH

    SELECT  name, 
            COALESCE(COUNT(transactions.subscID), 0) AS num_of_trns,
            COALESCE(d.balance,0)
    FROM    subscribers
            LEFT JOIN transactions
                ON subscribers.subscID = transactions.subscID 
                    AND YEAR(transactions.date) = 2012 AND
                        MONTH(transactions.date) = 3
            LEFT JOIN
            (
                SELECT  a.subscID, a.balance
                FROM    transactions a
                        INNER JOIN
                        (
                            SELECT subscID, MAX(date) maxDate
                            FROM transactions
                            WHERE YEAR(transactions.date) = 2012 AND
                                  MONTH(transactions.date) = 3
                            GROUP BY subscID
                        ) b ON a.subscID = b.subscID AND
                                a.date = b.maxDate
    
            ) d ON subscribers.subscID = d.subscID
    GROUP BY subscribers.subscID
    ORDER BY Name
    
    • SQLFiddle Demo

    SOURCE

    • COALESCE
    • MONTH
    • YEAR
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would you please help me to find out better solution for the following issue...
the following code is not behaving like I would expect. Please help me understand
http://www.avenuesocial.com/ plz check this site and please help me to find out this slider
I would request you to please help me out and guide me to the
Would you please help me getting back the typename of the original object put
Would you please help me with the issue below? I've tried 3 days without
Please help this would be my last problem in dealing with access database with
Would anyone please be able to tell me the most efficient way to compare
Please help me understand why this isn't working. I don't know if there is
yes, this is a homework-type question but could you please help me out? In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.