I have a with my SELECT statement that I just can't figure out. The query is as follows:
SELECT
count(1),
interaction_type_id
FROM
tibrptsassure.d_interaction_sub_type
GROUP BY
interaction_type_id
HAVING
count(interaction_type_id) > 1
ORDER BY
count(interaction_type_id) DESC
LIMIT 5;
Since my application does not support the use of the LIMIT keyword, I tried changing my query using the rank() function like so:
SELECT
interaction_type_id,
rank() OVER (PARTITION BY interaction_type_id ORDER BY count(interaction_type_id)
DESC)
FROM
tibrptsassure.d_interaction_sub_type;
However, this way I ended up with the following error message:
ERROR: column "d_interaction_sub_type.interaction_type_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT interaction_type_id, rank() OVER (PARTITION BY inter...
^
********** Error **********
ERROR: column "d_interaction_sub_type.interaction_type_id" must appear in the GROUP BY clause or be used in an aggregate function
SQL state: 42803
Character: 9
Is there an equivalent of rownum() in PostgreSQL? (Apart from using the LIMIT keyword to achieve the same result, that is.)
Does anybody have any suggestions for me? Thanks in advance.
row_number()function in postgresql:select row_number() over().rownumberis not quite the same asrank. Only the latter can provide a correct ranking in the case when two rows share the same rank.