Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Calculating percentage of records with SQL
Message
De
18/09/2001 09:28:57
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00557394
Message ID:
00557770
Vues:
9
Yes,
I just thought there might be deleted records and set deleted on situation.
Mine was a fancy long solution :) I could make it shorter (and one pass) getting all count and percents on a line too :
select ;
	sum(iif(sex='M',1,0)) as 'CountM', ;
	sum(iif(sex='M',1,0))/count(*)*100 as 'PercentM',;
	sum(iif(sex='F',1,0)) as 'CountF', ;
	sum(iif(sex='F',1,0))/count(*)*100 as 'PercentF' from mycursor
Honestly my preferred solution is at bottom of my first reply.
Cetin


>This might run very slowly, but I it seems to work:
>
>SELECT SEX, count(sex) as iGroupCt, ;
> (COUNT(*) / RECCOUNT() * 100) AS nPct FROM folks ;
> GROUP BY sex ;
> ORDER BY nPct DESC
>
>You might store RECCOUNT() to a variable before the SQL-SELECT to avoid taking that hit:
>
>liRecCount = RECCOUNT()
>SELECT SEX, count(sex) as iGroupCt, ;
> (COUNT(*) / liRecCount * 100) AS nPct FROM folks ;
> GROUP BY sex ;
> ORDER BY nPct DESC
>
>
>
>
>>>i have like this SQL syntax
>>>
>>>select sex,count(sex) from f:\okul_win\okul2 ;
>>>group by sex;
>>>order by sex;
>>>into curs mycurs
>>>brow
>>>use in mycurs
>>>
>>>and it shows in browse mode
>>>
>>>sex cnt_sex
>>>M 12
>>>W 5
>>>
>>>but i want to calculate percentage of M W counts of total records
>>>
>>>total records =17
>>>M=12/17=0,71
>>>M=5/17=0,29
>>>
>>>it must show in browse mode
>>>
>>>sex cnt_sex percentage
>>>M 12 0,71
>>>W 5 0,29
>>>
>>>but how ?
>>>
>>>thanks for the help
>>
>>Soykan,
>>Normally you should count totals separate and then grouping. For this particular situation where you know only 'M' and 'F' exists you could do it with this one SQL too :
>>
>>
select 'M' as sex, ;
>>	sum(iif(sex='M',1,0)) as 'Count', ;
>>	sum(iif(sex='M',1,0))/count(*) as 'Percent' from mycursor  ;
>>union ;
>>select 'F' as sex, ;
>>	sum(iif(sex='F',1,0)) as 'Count', ;
>>	sum(iif(sex='F',1,0))/count(*) as 'Percent' from mycursor
>>
PS: Not the way to go just a variation.
>>
>>select cnt(*) from mycursor into array arrTot
>>select sex, count(sex), count(sex)/arrTot from mycursor group by 1
>>
>>Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform