Well, you can do this without the CTE:
SELECT ' You have ' || points_cat1 ||' points and there are '|| tot-rnk || ' ahead of you '
FROM (SELECT ID, points_cat1,
rank() OVER (ORDER BY points_cat1 DESC ) AS RNK ,
count(*) over () as TOT
FROM user_pointS
) RANKED
WHERE ID = 10;
You can do it for all three catagories at once:
SELECT ' You have ' || points_cat1 ||' points and there are '|| tot-rnk1 || ' ahead of you ',
' You have ' || points_cat2 ||' points and there are '|| tot-rnk2 || ' ahead of you ',
' You have ' || points_cat3 ||' points and there are '|| tot-rnk3 || ' ahead of you '
FROM (SELECT ID, points_cat1, points_cat2, points_cat3,
rank() OVER (ORDER BY points_cat1 DESC ) AS RNK ,
rank() OVER (ORDER BY points_cat2 DESC ) AS RNK1 ,
rank() OVER (ORDER BY points_cat3 DESC ) AS RNK2 ,
count(*) over () as TOT
FROM user_pointS
) RANKED
WHERE ID = 10;
You can probably replace the tot-rnk
with an inverse ranking:
rank() OVER (ORDER BY points_cat1 ASC ) AS RNK
but you would want to test that to be sure it gives you the results you expect.