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-10-27 : 13:18:32
|
Hi,Can someone help? I have this query:SELECT t1.strNomObjet,t1.strNomDescripteur,sum((CAST(strReponse as decimal(15,2))) / strReponsePourcentage * 100) /t2.PersonCount AS ReponseCount FROM @Temp as t1INNER JOIN (SELECT strNomObjet,strNomDescripteur,count(*) AS PersonCount FROM @Temp GROUP BY strNomObjet,strNomDescripteur)t2 ON t2.strNomObjet=t1.strNomObjet AND t2.strNomDescripteur=t1.strNomDescripteurGROUP BY t1.strNomObjet,t1.strNomDescripteur,t2.PersonCount It working fine the only thing i need to do is :Instead of just using :sum((CAST(strReponse as decimal(15,2))) / strReponsePourcentage * 100) /t2.PersonCount AS ReponseCount I need if strReponse larger then 1000 do something elseIn my select if strReponse is larger then 1000 dosum((CAST(strReponse as decimal(15,2)) - 1000) / strReponsePourcentage * 100) /t2.PersonCount AS ReponseCount elsesum((CAST(strReponse as decimal(15,2))) / strReponsePourcentage * 100) /t2.PersonCount AS ReponseCount I hope i explain my self write is there a way to do it with case or??Tks in advanceLuc |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-27 : 13:22:31
|
| CASE WHEN strReponse > 1000 then .....else....end as ReponseCount |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2008-10-27 : 13:27:59
|
quote: Originally posted by hanbingl CASE WHEN strReponse > 1000 then .....else....end as ReponseCount
Hi tks for your quick responseI never use case before with sqlDo you mean somthing like this?It giving me an error "Column '@Temp.strReponse' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."Tks for your helpSELECT t1.strNomObjet,t1.strNomDescripteur,CASE WHEN (strReponse > 1000) then sum((CAST(strReponse as decimal(15,2)) - 1000) / strReponsePourcentage * 100) /t2.PersonCount else sum((CAST(strReponse as decimal(15,2))) / strReponsePourcentage * 100) /t2.PersonCount endAS ReponseCount FROM @Temp as t1INNER JOIN (SELECT strNomObjet,strNomDescripteur,count(*) AS PersonCount FROM @Temp GROUP BY strNomObjet,strNomDescripteur)t2 ON t2.strNomObjet=t1.strNomObjet AND t2.strNomDescripteur=t1.strNomDescripteurGROUP BY t1.strNomObjet,t1.strNomDescripteur,t2.PersonCount |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-27 : 13:33:13
|
may be this:-SELECT t1.strNomObjet,t1.strNomDescripteur,sum(CASE WHEN (strReponse > 1000) then ((CAST(strReponse as decimal(15,2)) - 1000) / strReponsePourcentage * 100) /t2.PersonCount else ((CAST(strReponse as decimal(15,2))) / strReponsePourcentage * 100) /t2.PersonCount end)AS ReponseCount FROM @Temp as t1INNER JOIN (SELECT strNomObjet,strNomDescripteur,count(*) AS PersonCount FROM @Temp GROUP BY strNomObjet,strNomDescripteur)t2 ON t2.strNomObjet=t1.strNomObjet AND t2.strNomDescripteur=t1.strNomDescripteurGROUP BY t1.strNomObjet,t1.strNomDescripteur,t2.PersonCount |
 |
|
|
lucsky8
Posting Yak Master
105 Posts |
Posted - 2008-10-27 : 14:49:40
|
| Hi tks it work perfect |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-28 : 00:29:12
|
quote: Originally posted by lucsky8 Hi tks it work perfect
cheers |
 |
|
|
|
|
|
|
|