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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 AVG where value != 0

Author  Topic 

Raskolnikov
Starting Member

8 Posts

Posted - 2009-07-21 : 14:29:47
I am trying to generate some statistics via one query, but i am not sure if it is possible.

Basically I have two tables playerscores and tournaments.


SELECT
MAX(t.majorCatId) AS tCat,
MAX(t.name) AS tName,
COUNT(t.name) AS tTimes,
AVG(ps.round1) as avgRd1,
AVG(ps.round2) as avgRd2,
AVG(ps.round3) as avgRd3,
AVG(ps.round4) as avgRd4,
AVG(ps.rank) as avgRank,
MAX(ps.rank) AS lowRank,
MIN(ps.rank) AS highRank,
MAX(ps.round1+ps.round2+ps.round3+ps.round4) AS highScore,
MIN(ps.round1+ps.round2+ps.round3+ps.round4) AS lowScore,
AVG(ps.round1+ps.round2+ps.round3+ps.round4) AS avgScore
FROM playerscores ps
JOIN tournaments t ON t.tournamentId=ps.tournamentId
WHERE ps.playerId = 2
GROUP BY t.majorCatId
ORDER BY MAX(t.startDate)


This works fine getting me info. However, it does not filter out zero values.

for instance AVG(ps.round1) is looking at three values:

70, 71, 0

It is giving me the average of the three. is there a way i can say AVG(ps.round1 WHERE ps.round1!=0)?

maybe something else? I have another question about this SQL, but will save it till i get an answer on this one.

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2009-07-21 : 14:35:20
AVG(case when ps.round1 > 0 then ps.round1 else NULL end)


elsasoft.org
Go to Top of Page

Raskolnikov
Starting Member

8 Posts

Posted - 2009-07-21 : 14:38:15
Thanks jezemine

Standard procedure here, I search for something for 10 minutes. Make a post, and then find a solution. I found this thread dealing with this issue:

[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=127151[/url]

Same idea as yours they just used NULLIF
Go to Top of Page

Raskolnikov
Starting Member

8 Posts

Posted - 2009-07-21 : 14:46:28
I have now utilized both methods in the query. I have posted the new query below for future reference example:


SELECT
MAX(t.majorCatId) AS tCat,
MAX(t.name) AS tName,
COUNT(t.name) AS tTimes,
AVG(NULLIF(ps.round1,0)) as avgRd1,
AVG(NULLIF(ps.round2,0)) as avgRd2,
AVG(NULLIF(ps.round3,0)) as avgRd3,
AVG(NULLIF(ps.round4,0)) as avgRd4,
AVG(NULLIF(ps.rank,0)) as avgRank,
MAX(NULLIF(ps.rank,0)) AS lowRank,
MIN(NULLIF(ps.rank,0)) AS highRank,
MAX(CASE
WHEN ps.round1>0 AND ps.round2>0 AND ps.round3>0 AND ps.round4>0 THEN (ps.round1+ps.round2+ps.round3+ps.round4)
END
) AS highScore,
MIN(CASE
WHEN ps.round1>0 AND ps.round2>0 AND ps.round3>0 AND ps.round4>0 THEN (ps.round1+ps.round2+ps.round3+ps.round4)
END
) AS lowScore,
AVG(
CASE
WHEN ps.round1>0 AND ps.round2>0 AND ps.round3>0 AND ps.round4>0 THEN (ps.round1+ps.round2+ps.round3+ps.round4)
END
) AS avgScore
FROM playerscores ps
JOIN tournaments t ON t.tournamentId=ps.tournamentId
WHERE ps.playerId = 2
GROUP BY t.majorCatId
ORDER BY MAX(t.startDate)
Go to Top of Page
   

- Advertisement -