Skip to content Skip to sidebar Skip to footer

SQL Ordering By Rating/votes

I have the following SQL statement; SELECT * FROM items WHERE votes > 4 ORDER BY (rating/votes) DESC But instead of it only showing WHERE votes > 4, i'd like it to display a

Solution 1:

It's a little unclear what you're going for, but I would probably have two parts to the ORDER BY clause:

  • Prefer ones with over four
  • Sort those

That would look something like this:

SELECT *
FROM items
ORDER BY IIF(Votes > 4, 0, 1),
         (rating/votes) DESC

Essentially, I assign a number for whether there are more than four votes (0), and one for if there are fewer (1). Since 0 is less than 1, the ones with more than four votes will be pulled first.

You probably don't want a WHERE clause for this, because it will filter out any with fewer than four votes.


Post a Comment for "SQL Ordering By Rating/votes"