Jump to content

Counting records by date


CGRRay

Recommended Posts

I have a table full of members who have joined our organization over the past several years and continue to join and renew. Their membership lasts for a specific amount of time, usually 1 year or 6 months. I need to write a query that will tell me how many members there were at the end of each month by chapter, for each month of this year.

All the records are stored in two tables called member and chapter. The fields in member are: member_id, first_name, last_name, date_joined, date_renewed, date_expires, chapter_id. The fields in chapters are: chapter_id, member_id, title.
I tried using this:
SELECT chapter.title,
(SELECT COUNT(*)
FROM member
WHERE member.chapter_id = chapter.chapter_id
AND member.date_expires > 1138751999) AS members
FROM chapter
ORDER BY chapter.title;
  but then realized that I was counting all the people who joined or renewed after Jan 31 too.
I sure would appreciate some help.
Thanks
Link to comment
Share on other sites

Thanks for responding. I found a different way to do this:
SELECT chapter.title,
(SELECT COUNT(*)
FROM member
WHERE member.chapter_id = chapter.chapter_id
AND member.date_expires > 1146441599) AS members_as_of_Mar31,
(SELECT COUNT(*)
FROM member
WHERE member.chapter_id = chapter.chapter_id
AND member.date_expires BETWEEN 1146441600 AND 1162339199) AS expired_after_Mar31,
(SELECT COUNT(*)
FROM member
WHERE member.chapter_id = chapter.chapter_id
AND member.date_joined BETWEEN 1146441600 AND 1162339199) AS joined_after_Mar31,
(SELECT COUNT(*)
FROM member
WHERE member.chapter_id = chapter.chapter_id
AND member.date_renewed BETWEEN 1146441600 AND 1162339199) AS renewed_after_Mar31
FROM chapter
ORDER BY chapter.title;

The results in MySQL Query Browser look correct. The problem is that this query works in MySQL Query Browser but not in my live version of MySQL. I get this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Any Ideas?
Thnaks again
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.