Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Calculating percentage of records with SQL
Message
 
À
18/09/2001 05:03:13
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:
00557756
Vues:
11
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
"Problems cannot be solved at the same level of awareness that created them." - Albert Einstein

Bruce Allen
NTX Data
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform