select min(cYear+cMonth) as cMinYearMonth, max(cYear+cMonth) as cMaxYearMonth from InputTable into cursor curTemp create table tblPeriods free (cYearMonth C(6), cYear C(4), cMonth C(2)) select curTemp m.cYearMonth=cMinYearMonth m.cYear=left(cMinYearMonth,4) lnMinYear=val(lcMinYear) m.cMonth=right(cMinYearMonth,2) lnMinMonth=val(lcMinMonth) lcMaxYear=left(cMaxYearMonth,4) lnMaxYear=val(lcMaxYear) lcMaxMonth=right(cMaxYearMonth,2) lnMaxMonth=val(lcMaxMonth) use in curTemp && Close temp cursor do while .t. insert into tblPeriods from memvar && First min record if lnMinYear=lnMaxYear and lnMinMonth=lnMaxMonth exit endif if lnMinMonth=12 lnMinMonth=0 lnMinYear=lnMinYear+1 endif lnMinMonth=lnMinMonth+1 m.cMonth=padl(lnMinMonth,2,'0') m.cYear=str(lnMinYear,4) m.cYearMonth=m.cYear+m.cMonth enddo Now I have all possible periods in tblPeriods table and may continue with your code. <sup>*</sup> All this code I wrote from the top of my head, so it's not necessary the best way... >>Thanks, Ko. Yes, a simple typo led to a problem :) >> >>BTW, the problem turns to be a little bit more complicated. >>Suppose, I select records from our database for this period of time: >>06/01/1997 to 11/30/2000 >> >>I'd like to have data on each possible period: each month, each quoter, each year in this time frame. How can I create a table with all these periods (I can not garantee, that all these periods would be in selected result). ><hr> >The last thing you can't do with your first solution (not having all periods) > >I've done my best and here is the result. >Please take a look at it, and tell me if it is what you need. >The data i used is below the code. > >Code follows:<hr> ><pre>ldFirstDate = {^1999-01-11} >ldLastDate = {^2001-01-11} >** The above should be parameters >Local lcOldCentury, lcOldDate,; && Old Settings > lcFirstMonth, lcLastMonth && Variables for selecting a date range > >lcOldCentury = Set('Century') >lcOldDate = Set('Date') >Set Date American >Set Century On > >lcFirstMonth = Right(DTOC(ldFirstDate),4) + Left(DTOC(ldFirstDate),2) >lcLastMonth = Right(DTOC(ldLastDate) ,4) + Left(DTOC(ldLastDate) ,2) > >Set Date &lcOldDate >Set Century &lcOldCentury > >** Preselections >** These are used later for determining what has to be selected >Select Distinct cYear; > From InputTable; > Order By cYear; > Where cYear+cMonth between lcFirstMonth and lcLastMonth; > ; && Probably You want the full year here but that's easy to change > Into Table tmpYear >Select Distinct cYear,cQtr; > From InputTable; > Where cYear+cMonth between lcFirstMonth and lcLastMonth; > ; && Probably You want the full Quater here but that's easy to change > Order By cYear,cQtr; > Into Table tmpQtr >Select Distinct cYear,cMonth; > From InputTable; > Where cYear+cMonth between lcFirstMonth and lcLastMonth; > ; && The day was not in the table, so i think you want this. > Order By cYear,cMonth; > Into Table tmpMonth > >#IF .F. && The three seperate select statements. >** Set the above to .T. to let them run >** The empty values are for the union thing later on. > Select cCode, cTown, tmpYear.cYear, ' ' as cQtr, ' ' as cMonth,; > Sum(iPrice) as YtdVol, Count(*) as YtdNum,; > tmpYear.cYear as tmpOrder; > From tmpYear, InputTable; > Where InputTable.cYear = tmpYear.cYear and; > InputTable.cYear+cMonth between lcFirstMonth and lcLastMonth; > ; && Probably You want the full Year here but that's easy to change > ; && This where clause is also added here, because otherwise all months of the > ; && year are included > Group By cCode, cTown, tmpOrder > > Select cCode, cTown, tmpQtr.cYear, tmpQtr.cQtr, ' ' as cMonth,; > Sum(iPrice) as YtdVol, Count(*) as YtdNum,; > tmpQtr.cYear+tmpQtr.cQtr as tmpOrder; > From tmpQtr, InputTable; > Where InputTable.cYear = tmpQtr.cYear and; > InputTable.cQtr between '01' and tmpQtr.cQtr and; > InputTable.cYear+cMonth between lcFirstMonth and lcLastMonth; > ; && Probably You want the full Quater here but that's easy to change > ; && This where clause is also added here, because otherwise all months of the > ; && quater are included > Group By cCode, cTown, tmpOrder > > Select cCode, cTown, tmpMonth.cYear, ' ' as cQtr, tmpMonth.cMonth,; > Sum(iPrice) as YtdVol, Count(*) as YtdNum,; > tmpMonth.cYear+tmpMonth.cMonth as tmpOrder; > From tmpMonth, InputTable; > Where InputTable.cYear = tmpMonth.cYear and; > InputTable.cMonth between '01' and tmpMonth.cMonth; > Group By cCode, cTown, tmpOrder >#Else && All results in one SQL > Select cCode, cTown, tmpYear.cYear, ' ' as cQtr, ' ' as cMonth,; > Sum(iPrice) as YtdVol, Count(*) as YtdNum,; > tmpYear.cYear as tmpOrder; > From tmpYear, InputTable; > Where InputTable.cYear = tmpYear.cYear and; > InputTable.cYear+cMonth between lcFirstMonth and lcLastMonth; > ; && Probably You want the full Year here but that's easy to change > ; && This where clause is also added here, because otherwise all months of the > ; && year are included > Group By cCode, cTown, tmpOrder; > Union; > Select cCode, cTown, tmpQtr.cYear, tmpQtr.cQtr, ' ' as cMonth,; > Sum(iPrice) as YtdVol, Count(*) as YtdNum,; > tmpQtr.cYear+tmpQtr.cQtr as tmpOrder; > From tmpQtr, InputTable; > Where InputTable.cYear = tmpQtr.cYear and; > InputTable.cQtr between '01' and tmpQtr.cQtr and; > InputTable.cYear+cMonth between lcFirstMonth and lcLastMonth; > ; && Probably You want the full Quater here but that's easy to change > ; && This where clause is also added here, because otherwise all months of the > ; && quater are included > Group By cCode, cTown, tmpOrder; > Union; > Select cCode, cTown, tmpMonth.cYear, ' ' as cQtr, tmpMonth.cMonth,; > Sum(iPrice) as YtdVol, Count(*) as YtdNum,; > tmpMonth.cYear+tmpMonth.cMonth as tmpOrder; > From tmpMonth, InputTable; > Where InputTable.cYear = tmpMonth.cYear and; > InputTable.cMonth between '01' and tmpMonth.cMonth; > Group By cCode, cTown, tmpOrder; > Order by 1,2,8 && cCode, cTown > && And then first the full year, then the results per month, and then per quater >#EndIf