MySQL Forums
Forum List  »  Newbie

Grouping by a date period.
Posted by: R A
Date: August 19, 2005 07:50AM

Let's say I have a table with dates that range from January to May in each record.
Is it possible to group the records by a date range, for example:

I want to group the records by a date period of 1 month, starting at the 3rd day of each month. So what I'd want to see is, five records for each month (jan 3 to feb 3, feb 3 to march 3, march 3 to april 3, april 3 to may 3).

At the moment I have the following:

select id, _date, sum(cost) from test
where
_date >= ' 2005-01-03' and
_date <= date_add('2005-01-03', interval 1 month)
group by extension
union
select extension, _date, sum(cost) from test
where
_date >= date_add(' 2005-01-03', interval 1 month) and
_date <= date_add('2005-01-03', interval 2 month)
group by extension

The trouble with this is that each month interval has its own select query and that the increase in the month is hard coded (interval 1 month for example). Is it possible to use SQL to inrement the month?

Thanks in advance ... (hope this made sense).

Options: ReplyQuote


Subject
Written By
Posted
Grouping by a date period.
R A
August 19, 2005 07:50AM
R A
August 19, 2005 08:13AM
R A
August 19, 2005 09:21AM
R A
August 19, 2005 11:29AM
R A
August 19, 2005 12:18PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.