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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help me with this query

Author  Topic 

boogi88
Starting Member

5 Posts

Posted - 2010-03-01 : 08:20:33
select count(*) as nor, sum(rating) as totalPoints, rating = CASE ((sum(rating))/(count(*)))
when round(((sum(rating))/(count(*))), 2) = 5 then 1
when round(((sum(rating))/(count(*))), 2) = 4 then 2
when round(((sum(rating))/(count(*))), 2) = 3 then 3
when round(((sum(rating))/(count(*))), 2) = 2 then 4
when round(((sum(rating))/(count(*))), 2) <= 1 then 5
ELSE 10
END as points, sum(rating)/count(*) as cond2
from tblratings where ratingType='Blog' and ratingParentID='113'


result
nor totalPoints points cond2
18 90 0 5


if u see this query cond2 gives result 5 but the condition did not return the proper value....

Help me find out y??

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-01 : 08:26:32
You have condition in CASE and also in WHEN that isn't ok.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

boogi88
Starting Member

5 Posts

Posted - 2010-03-01 : 08:28:17
didn't get u..... could u be more specific??
Go to Top of Page

boogi88
Starting Member

5 Posts

Posted - 2010-03-01 : 08:30:34
Actually thr is no condition in case.....
Please take a closer look at the query....
If i m wrong please post the correct query.....
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-03-01 : 08:33:14
quote:
Originally posted by boogi88

didn't get u..... could u be more specific??




Try this
select count(*) as nor, sum(rating) as totalPoints, CASE ((sum(rating))/(count(*)))
when 5 then 1
when 4 then 2
when 3 then 3
when 2 then 4
when <= 1 then 5
ELSE 10
END as points, sum(rating)/count(*) as cond2
from tblratings where ratingType='Blog' and ratingParentID='113'

PBUH
Go to Top of Page

boogi88
Starting Member

5 Posts

Posted - 2010-03-01 : 08:37:42
This is wat i got......

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '<= 1 then 5
ELSE 10
END as points, sum(rating)/count(*) as cond2
from tblrati' at line 6
Go to Top of Page

boogi88
Starting Member

5 Posts

Posted - 2010-03-01 : 08:48:15
Thanks.... it worked Gr8....
Here is my finished query.......


select count(ratingID) as nor, sum(rating) as totalPoints, round((sum(rating))/(CASE round(((sum(rating))/(count(ratingID))))
when 5 then 1
when 4 then 2
when 3 then 3
when 2 then 4
when 1 then 5
END)) as points, sum(rating)/count(ratingID) as cond2
from tblratings where ratingType='Blog' and ratingParentID='113'
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-03-01 : 08:53:17
No but u have missed the <= part.You can modify ur query this way
select count(*) as nor, sum(reading) as totalPoints,
CASE when AVG(Reading)=5 then 1
.
.
.
CASE when <= 1 then 5
ELSE 10
END as points, sum(rating)/count(*) as cond2
from tblratings where ratingType='Blog' and ratingParentID='113'

And I beleive u need average so u can directly use avg function instead of doing (sum(rating))/(count(*))the way I did.



PBUH
Go to Top of Page
   

- Advertisement -