Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAbe Layee
8,378 PointsI am trying to get the average in descending order from three column.
My goal here is to get the average of each state in descending order. I don't what I am doing wrong... this is the link of the image https://gyazo.com/cd62d7fce273deaa987471b6b936c129
SELECT state,
AVG(perDem),
AVG(perGOP),
AVG(perInd)
FROM polls
GROUP BY state
ORDER BY perDem DESC // this returns the perDem in desc
SELECT state,
AVG(perDem),
AVG(perGOP),
AVG(perInd)
FROM polls
GROUP BY state
ORDER BY perDem, perGop, perInd DESC // does not work
2 Answers
Steven Parker
231,248 PointsYou're displaying the averages, but ordering by the original column.
Also, you need to specify the ordering for each column.
ORDER BY perDem, perGop, perInd DESC -- does not work
--
ORDER BY AVG(perDem) DESC, AVG(perGOP) DESC, AVG(perInd) DESC -- but THIS should!
Another approach would be to use column aliases:
SELECT state,
AVG(perDem) AS perDem,
AVG(perGOP) AS perGop,
AVG(perInd) AS perInd
FROM polls
GROUP BY state
ORDER BY perDem DESC, perGop DESC, perInd DESC -- NOW it should work
Abe Layee
8,378 PointsThank you for your help