Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2008-06-03 : 13:20:34
|
Hi i create a temp table that count how many A,B,C and D there where in a table.I need calculate the percentage.It could be easy put the problem is that i cannot devide by the same number.Here my temp table DECLARE @Temp table ( strLastName varchar(50), strFirstName varchar(50), strReponse char(10), strNomObjet nvarchar(200), strDescripteurOrdre char(10), strNomDescripteur nvarchar(200), strObjetOrdre char(10) ) I want to check how many A there is.SELECT strNomObjet,strNomDescripteur,count(*) FROM @TempWHERE strReponse = 'A'GROUP BY strNomObjet,strNomDescripteur This will give me somthing like thisFor the first one there 25 ASecond 27 ANOMBRE ET OPÉRATIONS démontre une compréhension du concept du nombre 25NOMBRE ET OPÉRATIONS peut faire les opérations sur les nombres pour résoudre des problèmes 27STATISTIQUES ET PROBABILITÉS peut suivre les étapes d'une démarche statistique 26RÉGULARITÉS ET RELATIONS utilise des régularités (suites logiques) pour résoudre des problèmes 24RÉGULARITÉS ET RELATIONS utilise des relations mathématiques (algèbre) pour résoudre des problèmes 23FORMES ET ESPACE (géométrie) utilise la mesure pour résoudre des problèmes 17FORMES ET ESPACE (géométrie) utilise les figures géométriques pour résoudre des problèmes 22STATISTIQUES ET PROBABILITÉS utilise les probabilités pour résoudre des problèmes 27FORMES ET ESPACE (géométrie) utilise les transformations géométriques pour résoudre des problèmes 22After that i use SELECT count(*) FROM @TempGROUP BY strNomObjet,strNomDescripteur That will give me how many person have been tested.It will give me this363635363536363535So i need to take the first 2525 / 36 * 100 = 69.4%27 / 36 * 100 = 75%26 / 35 * 100 = 74.2%I hope you understand what i mean!Any idea?Tks in advance |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-03 : 13:29:34
|
| [code]SELECT t1.strNomObjet,t1.strNomDescripteur,t1.ReponseCount * 100.0/t2.PersonCount AS PercentageFROM(SELECT strNomObjet,strNomDescripteur,strReponse,count(*) AS ReponseCount FROM @TempGROUP BY strNomObjet,strNomDescripteur,strReponse)t1INNER JOIN (SELECT strNomObjet,strNomDescripteur,count(*) AS PersonCount FROM @TempGROUP BY strNomObjet,strNomDescripteur)t2ON t2.strNomObjet=t1.strNomObjetAND t2.strNomDescripteur=t1.strNomDescripteur[/code] |
 |
|
|
KevinKembel
Starting Member
11 Posts |
Posted - 2008-06-03 : 13:36:16
|
| I never tested this, and there's lots of ways you could do it, but I believe turning your two queries into subqueries and joining them on strNomObjet and strNomDescripteur will let you compare the two queries. Depending on your sql version, you could also look into the OVER clause, it might help?SELECT Results.strNomObjet, Results.strNomDescripteur, Results_A.total as total_a, Results.total as total, (Results_A.total / Results.total) AS pct_aFROM (SELECT strNomObjet, strNomDescripteur, count(*) as total FROM @Temp WHERE strResponse = 'A' GROUP BY strNomObjet, strNomDescripteur) AS Results_A , (SELECT strNomObjet, strNomDescripteur, count(*) as total FROM @Temp GROUP BY strNomObjet, strNomDescripteur) AS ResultsWHERE Results.strNomObjet = Results_A.strNomObjet AND Results.strNomDescripteur = Results_A.strNomDescripteur |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2008-06-03 : 13:50:36
|
| Hi tks both for the quick reply i will try and let you know! |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2008-06-03 : 13:55:08
|
| Tks it work great! i use tks again!SELECTResults.strNomObjet,Results.strNomDescripteur,Results_A.total as total_a,Results.total as total,(cast(Results_A.total as decimal(15,1)) / Results.total * 100) AS pct_aFROM (SELECT strNomObjet, strNomDescripteur, count(*) as totalFROM @TempWHERE strReponse = 'A'GROUP BY strNomObjet, strNomDescripteur) AS Results_A,(SELECT strNomObjet, strNomDescripteur, count(*) as totalFROM @TempGROUP BY strNomObjet, strNomDescripteur) AS ResultsWHERE Results.strNomObjet = Results_A.strNomObjetAND Results.strNomDescripteur = Results_A.strNomDescripteur |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-03 : 13:59:36
|
quote: Originally posted by lucsky8 Tks it work great! i use tks again!SELECTResults.strNomObjet,Results.strNomDescripteur,Results_A.total as total_a,Results.total as total,(cast(Results_A.total as decimal(15,1)) / Results.total * 100) AS pct_aFROM (SELECT strNomObjet, strNomDescripteur, count(*) as totalFROM @TempWHERE strReponse = 'A'GROUP BY strNomObjet, strNomDescripteur) AS Results_A,(SELECT strNomObjet, strNomDescripteur, count(*) as totalFROM @TempGROUP BY strNomObjet, strNomDescripteur) AS ResultsWHERE Results.strNomObjet = Results_A.strNomObjetAND Results.strNomDescripteur = Results_A.strNomDescripteur
You dont actually require to cast. Thats why i just multiplied by 100.0 to numerator in my post to avoid implicit conversion |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2008-06-03 : 14:02:04
|
| ok tks but if i did't cast it give me 0 any idea? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-03 : 14:31:33
|
quote: Originally posted by lucsky8 ok tks but if i did't cast it give me 0 any idea?
See my post on 06/03/2008 : 13:29:34 |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2008-06-03 : 15:18:17
|
| hey tks sorry my mistake work perfect! |
 |
|
|
|
|
|
|
|