Thanks for the reply, tripy.
Something similar to your query was the first thing that came to my mind when I was first handed the problem. However, it will only return clients that have used 10 or more times within the given range.
I am needing to find out what clients reached their 10th usage within a given date range. This means that they could have 9 other uses before the lower date in the date range, and one usage within the range.
I think I may have solved the problem, though. I'll do some more testing and post back exactly what I did.
It is something like this:
Code:
SELECT
c.client_id, c.client_name, c.client_register_date, max(u.usage_date) as maxdate, count(*) as totalusage
From
clientusage u
JOIN client c ON u.client_id= c.client_id
WHERE
u.usage_date <= '2008-11-01'
AND
c.cient_id IN
(
SELECT
c.client_id
FROM
clientusage u
JOIN client c ON u.client_id = c.client_id
WHERE
u.usage_date < '2008-09-01'
GROUP BY c.client_id
HAVING count(*) < 10
)
GROUP BY c.client_id, c.client_name, c.client_register_date
HAVING count(*) >= 10
What I did in the second select is grab all of the client IDs that had less than 10 usages before the lower of the 2 dates and then ensure they have 10 or more uses by the higher of the 2 dates.
I'm sure there is a more efficient way.
I could write some simple queries and handle most of the logic with my programming, but I would prefer to do as much work as possible within the query itself.
Thanks again for your reply!
|