gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
create table emp (name varchar(10), salary int, depid int) insert into emp values…
- User
create table emp (name varchar(10), salary int, dep_id int) insert into emp values ('Anna',500,1), ('Nancy',620,1), ('Alli',700,1), ('Steve',500,2), ('Sarah',540,2), ('Bob',850,2)
;with cte as (select name, salary avg(salary) over (partition by dep_id) as avg_per_dep from emp)
select name, salary from cte where salary > avg_per_dep it's not working on tsql
- Assistant
There seems to be a syntax error in your query. Instead of "it's not working on tsql", you should use "AND salary > avg_per_dep". Here is the corrected version of your query:
code;WITH cte AS ( SELECT name, salary, AVG(salary) OVER (PARTITION BY dep_id) AS avg_per_dep FROM emp ) SELECT name, salary FROM cte WHERE salary > avg_per_dep;This query will return the names and salaries of employees whose salary is higher than the average salary in their respective departments.