Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL Select with Group by and Order by
Message
 
 
To
27/04/2010 21:01:21
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 6 SP5
Miscellaneous
Thread ID:
01462119
Message ID:
01462297
Views:
39
>>
Select master_code, Count(*) As 'count' ;
>> FROM myTable ;
>> GROUP By master_code ;
>> INTO Cursor crsCounts
>>
>>Select t1.*, t2.Count ;
>> From myTable t1 ;
>> INNER Join crsCounts t2 ;
>> ON t1.master_code == t2.master_code ;
>> WHERE t1.master_ind = 'Y' Or t2.Count = 1
>>
>>PS: Having multiple SQL is both more manageable/readadble plus it is fast. Don't seek single SQL versions IMHO.
>>Cetin
>
>Yeah, I figured I could do it with multiple selects statements. I should have mentioned that the result set is going to be an updateable view that drives a form. Thanks for taking the time.
>
>Tim

In VFP9 you can combine two of them into one using derived tables.

In VFP6:
select max(Master_ind) as Master_Ind, Master_Code, count(*) as RecCount from myTable T1
where Master_Ind = 'Y' or not exists(select 1 from myTable T where T.Master_Code  = T1.Master_Code and Master_Ind = 'Y')
group by Master_Code
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform