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 - 2009-04-28 : 08:25:16
|
Hi,I have a stored procedure with a temp table .DECLARE @Temp table( strLastName varchar(50), strFirstName varchar(50), strReponse char(10), strReponsePourcentage char(10), strNomObjet nvarchar(200), strDescripteurOrdre char(10), strNomDescripteur nvarchar(200), strObjetOrdre char(10))SELECT COUNT(CASE WHEN ((CAST(strReponse as decimal(15,2))) / (CAST(strReponsePourcentage as decimal(15,2))) * 100) >= 65 THEN strReponse ELSE NULL END) AS [65+],COUNT(CASE WHEN CAST(strReponse as decimal(15,2)) != -1 THEN strReponse ELSE NULL END) AS [number of student],COUNT(CASE WHEN ((CAST(strReponse as decimal(15,2))) / (CAST(strReponsePourcentage as decimal(15,2))) * 100) >= 85 THEN strReponse ELSE NULL END) AS [85+]from @TempGROUP BY strNomObjet,strNomDescripteurORDER BY strNomObjet,strNomDescripteur The query give me this resultte first row a count how many student did 65% and morethe second is how many studentthe third is how many student did 85% and more31 34 1724 34 1630 34 2225 34 160 0 029 34 2228 34 1422 34 120 0 0Evrething is great but i need to had the % of student who did 65% and more and 85% and moreEx for the first row : (how many 65% and more / number of student * 100)31 / 34 * 100 = 91% "That the % for 65% and more17 / 31 * 100 = 50% "That the % for 85 and moreSo it would give something like this :31 34 17 91 50Tks in advance!Luc |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-04-28 : 08:29:51
|
| [code]Select [65+], [number of student], [85+], [65% and more]=[65+]*1.0/[number of student]*100, [85% and more]=[85+]*1.0/[number of student]*100from (SELECT COUNT(CASE WHEN ((CAST(strReponse as decimal(15,2))) / (CAST(strReponsePourcentage as decimal(15,2))) * 100) >= 65 THEN strReponse ELSE NULL END) AS [65+], COUNT(CASE WHEN CAST(strReponse as decimal(15,2)) != -1 THEN strReponse ELSE NULL END) AS [number of student], COUNT(CASE WHEN ((CAST(strReponse as decimal(15,2))) / (CAST(strReponsePourcentage as decimal(15,2))) * 100) >= 85 THEN strReponse ELSE NULL END) AS [85+] from @Temp GROUP BY strNomObjet,strNomDescripteur ORDER BY strNomObjet,strNomDescripteur)s[/code] |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2009-04-28 : 08:34:40
|
Hi tks for the quick reply!It give me this error :The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.And i forgot to mention it possible that it will be devision by 0 If i remove ORDER BY strNomObjet,strNomDescripteurIt give me :Warning: Null value is eliminated by an aggregate or other SET operation.quote: Originally posted by sakets_2000
Select [65+], [number of student], [85+], [65% and more]=[65+]*1.0/[number of student]*100, [85% and more]=[85+]*1.0/[number of student]*100from (SELECT COUNT(CASE WHEN ((CAST(strReponse as decimal(15,2))) / (CAST(strReponsePourcentage as decimal(15,2))) * 100) >= 65 THEN strReponse ELSE NULL END) AS [65+], COUNT(CASE WHEN CAST(strReponse as decimal(15,2)) != -1 THEN strReponse ELSE NULL END) AS [number of student], COUNT(CASE WHEN ((CAST(strReponse as decimal(15,2))) / (CAST(strReponsePourcentage as decimal(15,2))) * 100) >= 85 THEN strReponse ELSE NULL END) AS [85+] from @Temp GROUP BY strNomObjet,strNomDescripteur ORDER BY strNomObjet,strNomDescripteur)s
|
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2009-04-28 : 08:43:19
|
| Hi it workingi remove the order byand put ISNULLTksCan we use ORder by in subquery??Is there a way i can use Order by?Is ok if u use SELECT TOP 99Select [65+], [number of student], [85+], [65% and more]=[65+]*1.0/NULLIF([number of student], 0)*100, [85% and more]=[85+]*1.0/NULLIF([number of student], 0)*100from (SELECT COUNT(CASE WHEN ((CAST(strReponse as decimal(15,2))) / (CAST(strReponsePourcentage as decimal(15,2))) * 100) >= 65 THEN strReponse ELSE NULL END) AS [65+], COUNT(CASE WHEN CAST(strReponse as decimal(15,2)) != -1 THEN strReponse ELSE NULL END) AS [number of student], COUNT(CASE WHEN ((CAST(strReponse as decimal(15,2))) / (CAST(strReponsePourcentage as decimal(15,2))) * 100) >= 85 THEN strReponse ELSE NULL END) AS [85+] from @Temp GROUP BY strNomObjet,strNomDescripteur)s |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-04-28 : 08:53:31
|
| You can't use order by in sub query. Put it outside if you want it ordered. |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-04-28 : 08:54:53
|
here, Select [65+], [number of student], [85+], [65% and more]=[65+]*1.0/[number of student]*100, [85% and more]=[85+]*1.0/[number of student]*100from (SELECT COUNT(CASE WHEN ((CAST(strReponse as decimal(15,2))) / (CAST(strReponsePourcentage as decimal(15,2))) * 100) >= 65 THEN strReponse ELSE NULL END) AS [65+], COUNT(CASE WHEN CAST(strReponse as decimal(15,2)) != -1 THEN strReponse ELSE NULL END) AS [number of student], COUNT(CASE WHEN ((CAST(strReponse as decimal(15,2))) / (CAST(strReponsePourcentage as decimal(15,2))) * 100) >= 85 THEN strReponse ELSE NULL END) AS [85+], strNomObjet, strNomDescripteur from @Temp GROUP BY strNomObjet,strNomDescripteur )sORDER BY strNomObjet,strNomDescripteur |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2009-04-28 : 09:00:28
|
Tks working great!!!Have a nice day!quote: Originally posted by sakets_2000 here, Select [65+], [number of student], [85+], [65% and more]=[65+]*1.0/[number of student]*100, [85% and more]=[85+]*1.0/[number of student]*100from (SELECT COUNT(CASE WHEN ((CAST(strReponse as decimal(15,2))) / (CAST(strReponsePourcentage as decimal(15,2))) * 100) >= 65 THEN strReponse ELSE NULL END) AS [65+], COUNT(CASE WHEN CAST(strReponse as decimal(15,2)) != -1 THEN strReponse ELSE NULL END) AS [number of student], COUNT(CASE WHEN ((CAST(strReponse as decimal(15,2))) / (CAST(strReponsePourcentage as decimal(15,2))) * 100) >= 85 THEN strReponse ELSE NULL END) AS [85+], strNomObjet, strNomDescripteur from @Temp GROUP BY strNomObjet,strNomDescripteur )sORDER BY strNomObjet,strNomDescripteur
|
 |
|
|
|
|
|
|
|