I have a table tips that is defined as follows:
CREATE TABLE tips
(
tip_id bigserial NOT NULL,
tip text NOT NULL,
author text NOT NULL,
post_date bigint NOT NULL,
likers character varying(16)[],
dislikers character varying(16)[],
likes integer NOT NULL,
dislikes integer NOT NULL,
abuse_history character varying(16)[]
);
I need to get the tips based on popularity, with the definition of popularity being:
likes – dislikes – (size(abuse_history)*5)
The query below gives me the same results regardless of the sort order (ASC/DESC).
select * from tips order by (likes - dislikes - (array_length(abuse_history,1) * 5)) ASC limit 2147483647 offset 0
EDIT
I inserted 3 records that have the following values:
1) 1 like, 0 dislikes, 0 abuse complaints
2) 0 likes, 1 dislike, 0 abuse complaints
3) 0 likes, 0 dislikes, 0 abuse…
Regardless of the sort order (ASC/DESC), I get the following order:
{3, 1, 2}
Could anyone please point me in the right direction?
Consider this:
Output is
nullfor an empty array. Also, yourabuse_historycan benullitself. So you need something like this:fiddle
Sample output: