View Single Post

   
  #3 (permalink)  
Old 02-28-2008, 07:37 AM
Bosconian
 
Posts: n/a
Default Re: calculate using in-query values

"Bill Karwin" <bill@karwin.com> wrote in message
news:e26rjq025q8@enews2.newsguy.com...
> Bosconian wrote:
> > SELECT mr.teamid,
> > SUM( IF( mr.winner = '1', 1, 0 ) ) AS wins,
> > SUM( IF( mr.winner = '0', 1, 0 ) ) AS losses,
> > SUM(wins/losses) AS percentage <----- how to correctly calculate
> > FROM match_result mr
> > GROUP BY mr.teamid

>
> Would this do what you want?
>
> SELECT mr.teamid,
> SUM( IF( mr.winner = '1', 1, 0 ) ) / COUNT(mr.winner) AS wins_pct,
> SUM( IF( mr.winner = '0', 1, 0 ) ) / COUNT(mr.winner) AS losses_pct,
> FROM match_result mr
> GROUP BY mr.teamid
>
> Unfortunately, we cannot reference column aliases in other expressions
> in the select-list. We can reference column aliases only in GROUP BY,
> ORDER BY, or HAVING clauses. See
> http://dev.mysql.com/doc/refman/5.0/...ith-alias.html
>
> Regards,
> Bill K.


I opted to perform this calculation at the page level after all because of
additional considerations. I file this away for future use though--thanks!


Reply With Quote