MySql Query Involving Dates

I know this might not be the best place to ask MySql-related questions, but nevertheless hope someone could at least point me to the right direction.

I did a lot of googling around, but still ended up here.

The problem I have is this. I have a database set up in MySql that holds membership data for a small organization. One of the tables holds data about the members, like names, date joined and date left. In another table I keep track of decisions made about the monthly membership fees.

The challenge comes when I try to calculate the fees due for each member. Had it only been one membership fee valid after date X, I would not have had any trouble. However, if, say, membership fee has changed over time, I can’t figure out a way to calculate the amounts due so that each member’s contribution is calculated depending on the date he or she joined, and the fee amount as it is changing over time.

Is anyone aware of a guide in MySql that specifically discusses such issues?

I appreciate any feedback, and thank you in advance.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

This should be fairly simple if you have dates when fees are implemented
in your table of monthly membership fees. You could just compare
date_joined (user table) with date_implemented (fee table)… find the
smallest value of date_implemented that is after date_joined.

SELECT fees.amount FROM fees WHERE fees.date_implemented = (SELECT
MIN(fees.date_implemented) FROM fees WHERE fees.date_implemented >
users.date_joined) AND users.name=‘userNameHere’;

I think that will do it, though I’m not sure. Replace ‘userNameHere’
appropriately (along with table/column names.

Good luck.

hermesrules wrote:
> I know this might not be the best place to ask MySql-related questions,
> but nevertheless hope someone could at least point me to the right
> direction.
>
> I did a lot of googling around, but still ended up here.
>
> The problem I have is this. I have a database set up in MySql that
> holds membership data for a small organization. One of the tables holds
> data about the members, like names, date joined and date left. In
> another table I keep track of decisions made about the monthly
> membership fees.
>
> The challenge comes when I try to calculate the fees due for each
> member. Had it only been one membership fee valid after date X, I would
> not have had any trouble. However, if, say, membership fee has changed
> over time, I can’t figure out a way to calculate the amounts due so that
> each member’s contribution is calculated depending on the date he or she
> joined, and the fee amount as it is changing over time.
>
> Is anyone aware of a guide in MySql that specifically discusses such
> issues?
>
> I appreciate any feedback, and thank you in advance.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iQIcBAEBAgAGBQJJtTLKAAoJEF+XTK08PnB5XOcP/2zWQSyFON0OvsMKAKdsuLik
Q1cucbb75TcLmOtAg4QBViM/lMI9qPO4BJqxnY4gxDL98bPvZyPQWBcbe9YPArR5
XdL3oekCubzTuBbweaID/TP1jqQMWI1srRJ02K+GUnhSXuT157USO0nL0wdTJX8R
twpt/It2iEwauptDkupzRAS2oI14yk5/CzM+Mcu4CG6P3Ew9VuxfOJ/4fYkHw6Oj
qyrl4r6WcQf3Cj/iZJbyPrE2XPsOiso0p8Bru2/va6SHUUGjm/Y9srQjrx0ys1IM
RpdN10ti0hJIWq1PrJFpaXKOJpr5fmGkn+FSN6fRthQQeFhLrrPWALOlVjRvV+HB
v0d6386l33KNWc3AFOzlqSYgdQ94Aze38mrGPYAveTJ/q0q96fBcbAv0/6aor8KP
1kltZ0pTojNvXtFkuJMzNlw+gyf1ZyWs1Nku3PQ9y0CrA7ZnyG3beurhRuySxcKx
VkqwzGaSJBF9bxhYshgsi7DuO3lHYJH5BEPHEiTophZ/028rBfcpj0kFRjZAF4oF
LufVmQtf3f9T035sDILJ2zbPdQuvEMjsmRZB5vCKM0iueIpOcjK7rGqIsUab8cYZ
twh/596SACK2vdjsUUvoionTEkPgkTGLJojzizDFPYed+vbjw74ZfRy2V9/4zRxc
TG7d8s7wSONk7qYHpW3s
=s8yk
-----END PGP SIGNATURE-----

I always found my MySQL answers on: MySQL :: MySQL Documentation

Thanks for your suggestions, I do appreciate it. I found the logic to be easier that the actual sql notation, still willingly learning.

Umm…

How do your date ranges work? is it:

  • Some people joined before date ‘x’
  • Some people joined between date ‘x’ and date ‘y’
  • Some people joined after date ‘y’ ?

IF SO, then add the CASE statement to your select statement. In the following, ‘join_date’ is the field in your database that contains the date the member joined. And date ‘x’ is assumed to be 01/01/1990 and date ‘y’ is 26/07/1996. This example will return 10 or 20 or 30 respectively.

CASE join_date
WHEN < “19900101” THEN SELECT 10
WHEN between “19900101” and “19960726” THEN SELECT 20
ELSE SELECT 30
END CASE

any use??

  • Simon