I have the data like this:
CREATE TABLE salaries AS
SELECT * FROM ( VALUES
('US' , 'A', 3935),
('US' , 'B', 7805),
('US' , 'C', 2302),
('US' , 'D', 6772),
('US' , 'E', 3173),
('US' , 'F', 7739),
('Japan' , 'G', 3881),
('Japan' , 'H', 1158),
('Japan' , 'I', 2591),
('Japan' , 'J', 3758),
('Japan' , 'K', 8710),
('Japan' , 'L', 3376),
('France', 'M', 5768),
('France', 'N', 9466),
('France', 'O', 1750),
('France', 'P', 1049),
('France', 'Q', 3479),
('France', 'R', 5305)
) AS t(country,employee,salary);
In order to find difference between maximum salary and fifth highest salary for every country I am trying the following approach:
select max(salary) over (partition by country) - rank(5) over (partition by country)
from salaries
But it is throwing the following error:
"WITHIN GROUP is required for ordered-set aggregate function"
can anyone suggest any method without using any join?