Database Reference
In-Depth Information
GROUP BY answer;
+--------------+-------+---------+
| Birding Site | Votes | Percent |
+--------------+-------+---------+
| forest | 30 | 35% |
| shore | 42 | 49% |
| backyard | 14 | 17% |
+--------------+-------+---------+
That rounded up all of the values. If a value has no decimal places, it wouldn't change the
value.
Truncating Numbers
If we don't want to round anumber up or down, but we just want to eliminate the extra
decimal places, we canuse TRUNCATE() . Let's see how that looks with the same SQL
statement we've been modifying:
SELECT COLUMN_GET(choices, answer AS CHAR)
AS 'Birding Site',
COUNT(*) AS 'Votes',
CONCAT( TRUNCATE( (COUNT(*) / @fav_site_total) * 100, 1), '%')
AS 'Percent'
FROM survey_answers
JOIN survey_questions USING(question_id)
WHERE survey_id = 1
AND question_id = 1
GROUP BY answer;
+--------------+-------+---------+
| Birding Site | Votes | Percent |
+--------------+-------+---------+
| forest | 30 | 34.8% |
| shore | 42 | 48.8% |
| backyard | 14 | 16.2% |
+--------------+-------+---------+
As the name of the function implies, it truncated the value after the number of decimal
places specified (i.e., 1 in this example).
Eliminating Negative Numbers
Sometimes when we'reworking with numbers in functions, we get them in the wrong or-
der and the result is a number with a negative sign. If we're trying to find only the differ-
ence between two numbers, we canuse ABS() to return the absolute value, the value
Search WWH ::




Custom Search