This is a read-only copy of the MariaDB Knowledgebase generated on 2024-11-16. For the latest, interactive version please visit https://mariadb.com/kb/.

Getting pct from SUM data

I'm trying to get my sql working to get % from data from 2 sum'd columns IE : SELECT SUM(NumCalls) as SUM_NumCalls, SUM(NumCompleted) as SUM_NumCompleted , (SUM_NumCompleted * 100 / SUM_NumCalls ) as `% Completed` FROM my_schema.my_table GROUP BY Company ;

Error Code: 1054. Unknown column 'SUM_NumCompleted' in 'field list'

Answer Answered by Ian Gilfillan in this comment.

You cannot use the alias there, as it hasn't been evaluated yet. Something like:

SELECT 
  SUM(NumCalls) as SUM_NumCalls, 
  SUM(NumCompleted) as SUM_NumCompleted, 
  (SUM(NumCompleted) * 100 / SUM(NumCalls)) as  ...

should work.

Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.