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