Re: Windowing Function Patch Review -> Standard Conformance - Mailing list pgsql-hackers

From David Rowley
Subject Re: Windowing Function Patch Review -> Standard Conformance
Date
Msg-id E1304A0E35F34CB080A83F60AD9B71EF@amd64
Whole thread Raw
In response to Re: Windowing Function Patch Review -> Standard Conformance  ("Hitoshi Harada" <umi.tanuki@gmail.com>)
Responses Re: Windowing Function Patch Review -> Standard Conformance
List pgsql-hackers
Using one of my original test tables I'm testing windowing functions with a
GROUP BY.

The following query works as I would expect.

-- Works
SELECT department,      SUM(Salary),      ROW_NUMBER() OVER (ORDER BY department),      SUM(SUM(salary)) OVER (ORDER BY
department)
FROM employees
GROUP BY department;


The following one fails with the message.
ERROR:  variable not found in subplan target list

-- Does not work.
SELECT department,      SUM(Salary),      ROW_NUMBER() OVER (ORDER BY department),      SUM(SUM(salary)) OVER (ORDER BY
departmentDESC)
 
FROM employees
GROUP BY department;

I just added the DESC to force it into creating 2 separate windows.

I can re-write the non working query to work using the following:


SELECT department,      salary,      ROW_NUMBER() OVER (ORDER BY department),      SUM(salary) OVER (ORDER BY
departmentDESC)
 
FROM (SELECT department,            SUM(salary) AS salary     FROM employees     GROUP BY department
) t;




Testing with:

create table employees ( id INT primary key, name varchar(30) not null, department varchar(30) not null, salary int not
null,check (salary >= 0)
 
);


insert into employees values(1,'Jeff','IT',10000);
insert into employees values(2,'Sam','IT',12000);

insert into employees values(3,'Richard','Manager',30000);
insert into employees values(4,'Ian','Manager',20000);

insert into employees values(5,'John','IT',60000);
insert into employees values(6,'Matthew','Director',60000);



pgsql-hackers by date:

Previous
From: Greg Smith
Date:
Subject: Re: Simple postgresql.conf wizard
Next
From: "David Rowley"
Date:
Subject: Windowing Function Patch Review -> NTILE function