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
 Select Case null value then return 0

Author  Topic 

Kotti
Posting Yak Master

129 Posts

Posted - 2009-06-02 : 09:44:46
Hi,
I am wroking with following query,Here some column rating and avgrating return null value.But i need if rating is null then it return zero.
Following is test query.

SELECT r.*,rr.noofvoter,rr.Points,rr.avgrating,rr.Rating
FROM sf_videomgmt r left JOIN (SELECT VideoId,SUM(Points)AS Points,SUM(CASE WHEN Points=0 THEN 0 ELSE 1 END) AS noofvoter,
SUM(Points)/COUNT(VideoId) AS Rating,ISNULL(SUM(Points)*1.0/NULLIF(SUM(CASE WHEN Points=0 THEN 0 ELSE 1 END),0),0) AS
avgrating FROM sf_ratingmgmt GROUP BY VideoId) rr ON rr.VideoId=r.VideoId where r.filmtype=1 and r.deleted=0 and r.Status=1 and r.makepublic=1

Thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-02 : 09:46:55
isnull(your_result,0) as your_column_name


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

Kotti
Posting Yak Master

129 Posts

Posted - 2009-06-02 : 11:08:10
Yes,
It's working.Thanks for your help
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-02 : 11:57:24
i prefer coalesce() because of following reasons

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/10/04/isnull-or-coalesce.aspx
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-02 : 12:19:27
In this case (and for my postcounter ) i prefer isnull.


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

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-06-03 : 01:44:12
SUM(Points)/COUNT(VideoId)

should be

SUM(Points)*1.0/COUNT(VideoId)

to get proper result
Refer
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/01/16/beware-of-implicit-conversions.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -