Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Complicated SQL - can we make it?
Message
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00437328
Message ID:
00437995
Views:
26
>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).


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:
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


The data used for testing follows below.
I now used the fox command copy to, so there will be no typos <vbg>
cCode,cTown,cMonth,cQtr,cYear,iPrice

'01' 'TNZN' '01' 'Q1' '2000' 10
'01' 'HOEK' '01' 'Q1' '2000' 10
'01' 'TNZN' '04' 'Q2' '2000' 40
'02' 'HOEK' '01' 'Q1' '1999' 1
'02' 'TNZN' '04' 'Q2' '2000' 40
'02' 'HOEK' '02' 'Q1' '2000' 22
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform