Random order by but first 3
Hi,
Suppose I have a column t1 for a table. Now t1 holds some numerice value for each row. Say R1 to R5 records has values for the column t1 as :
t1(2,5,8,10,32)
I want the result to be printed as (10, 32, 8, 2, 5) means - Big, Biggest, small , <then any random order>
Regards,
Arup Rakshit
--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
On Thu, Jun 11, 2015 at 12:35 PM, Arup Rakshit <aruprakshit@rocketmail.com>
wrote:
Hi,
Suppose I have a column t1 for a table. Now t1 holds some numerice value
for each row. Say R1 to R5 records has values for the column t1 as :t1(2,5,8,10,32)
I want the result to be printed as (10, 32, 8, 2, 5) means - Big, Biggest,
small , <then any random order>
SELECT v FROM (
SELECT *,
CASE
when row_number = 2 THEN 1.0
WHEN row_number = 1 THEN 2.0
WHEN row_number = 3 THEN 3.0
ELSE 10.0 + random()
END AS custom_rank
FROM (
SELECT *, row_number() OVER (ORDER BY v DESC)
FROM ( VALUES (1),(2),(3),(4),(5) ) val (v)
) v_rank
) custom_rank
ORDER BY custom_rank
Feel free to mix in different window functions to deal with ties and the
like.
David J.