I have a table with name and grades for some users
CREATE TABLE grades (name varchar(100), grade integer);
insert into grades
values
('Bob', 12),
('Bob', 13),
('Bob', 23),
('Bob', 17),
('James', 15),
('James', 27),
('Nick ', 18),
('Nick ', 16),
('Nick ', 22),
('Nick ', 32),
('Nick ', 19);
I want an output table grouped by the name, along with the average grade and the nth lowest value for each name.
I tried to use window function nth_value() but I get an error when I try to execute the query (n = 2 here)
select name, avg(grade), nth_value(grade, 2) over(partition by name
order by grade
Range BETWEEN
UNBOUNDED PRECEDING AND
UNBOUNDED FOLLOWING)
from grades group by name;
Error(s), warning(s):
42803: column "grades.grade" must appear in the GROUP BY clause or be used in an aggregate function
What is the correct way of writing such a query?