Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Complex SQL problem
Message
From
06/09/2008 06:29:26
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01345309
Message ID:
01345318
Views:
16
>Suppose that I have a table called politics and with this folowing fields
>
>
>delegates: c(20)
>City:         c(20)
>State:       c(5)
>
>
>And I want to generate this sort or report. Can it be done with an SQL statement?
>(I know how to do it with a do while loop.)
>
>
>delegate, City
>Robert     Phoenix
>Smith     Snow Flake
>Burns     Snow Flake
>etc.
>           Az: Delegates: 3
>
>Stevens  San Diego
>Jerome   Los Angeles
>          Ca:  Delegates 2
>etc.
>etc.
>
>
>Thanks
>Steve


One way is to add a report variable if you group by state

Without report variable, add a field nDelegates. You can even print the #delegates at the top of the State group
	create cursor Politics ;
	(	Delegates	c(20), ;
		City		c(20), ;
		State		c(5) ;
	)
	
	insert into Politics values('Robert', 'Phoenix', 'AZ')
	insert into Politics values('Smith', 'Snow Flake', 'AZ')
	insert into Politics values('Burns', 'Snow Flake', 'AZ')
	
	insert into Politics values('Stevens', 'San Diego', 'CA')
	insert into Politics values('Jerome', 'Los Angeles', 'CA')
	
	
	select State, ;
		   count(*) as nDelegates ;
		from Politics ;
		into cursor tmp ;
		group by 1
	
	select	Politics.Delegates, ;
			Politics.City, ;
			Politics.State, ;
			tmp.nDelegates ;
		from Politics ;
			join tmp on (Politics.State == tmp.State ) ;
		into cursor result ;
		order by 3,2,1
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform