Thread: equivalent of oracle rank() in postgres

equivalent of oracle rank() in postgres

From
"Chandan_Kumaraiah"
Date:
<div class="Section1"><p class="MsoNormal"><font face="Courier" size="3"><span style="font-size:12.0pt;
font-family:Courier"> </span></font><p class="MsoNormal"><font face="Courier" size="3"><span style="font-size:12.0pt;
font-family:Courier">Hi,</span></font><p class="MsoNormal"><font face="Courier" size="3"><span style="font-size:12.0pt;
font-family:Courier"> </span></font><p class="MsoNormal"><font face="Courier" size="3"><span style="font-size:12.0pt;
font-family:Courier">Jus wanted the equivalent for rank() as in tis example..</span></font><p class="MsoNormal"><font
face="Courier"size="3"><span style="font-size:12.0pt; 
font-family:Courier"> </span></font><p class="MsoNormal"><font face="Courier" size="3"><span style="font-size:12.0pt;
font-family:Courier">SELECT *<br /> FROM (<br />   SELECT employee_id, last_name, salary,<br />   RANK() OVER (ORDER BY
salaryDESC) EMPRANK<br />   FROM employees)<br /> WHERE emprank = 3;</span></font><p class="MsoNormal"><font
face="Courier"size="3"><span style="font-size:12.0pt; 
font-family:Courier"> </span></font><p class="MsoNormal"><font face="Courier" size="3"><span style="font-size:12.0pt;
font-family:Courier">Rgds,</span></font><p class="MsoNormal"><font face="Courier" size="3"><span
style="font-size:12.0pt;
font-family:Courier">Chandan</span></font><font face="Arial" size="2"><span
style="font-size:10.0pt;font-family:Arial"></span></font></div>

Re: equivalent of oracle rank() in postgres

From
"Greg Sabino Mullane"
Date:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


> Jus wanted the equivalent for rank() as in tis example..

> SELECT *
> FROM (
>   SELECT employee_id, last_name, salary,
>   RANK() OVER (ORDER BY salary DESC) EMPRANK
>   FROM employees)
> WHERE emprank = 3;

There is no direct equivalent to rank(), but there are certainly
other ways to get the results. The above query can be written in
PostgreSQL as:

SELECT employee_id, last_name, salary
FROM employees
WHERE salary =(SELECT DISTINCT salary FROM employees ORDER BY salary DESC OFFSET 2 LIMIT 1);

- --
Greg Sabino Mullane greg@turnstep.com
PGP Key: 0x14964AC8 200503212152
http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8
-----BEGIN PGP SIGNATURE-----

iD8DBQFCP4hwvJuQZxSWSsgRAoKPAKDE0pB4NueE0Dh9EfJiXw79SvCDoACcC4xb
ydxVgK9DgGHQXJqFIrlHIIo=
=GRIX
-----END PGP SIGNATURE-----