Re: 2nd attempt: Window function SQL - can't quite figure it. - Mailing list pgsql-novice

From David G. Johnston
Subject Re: 2nd attempt: Window function SQL - can't quite figure it.
Date
Msg-id CAKFQuwY+w2H028pw9ajghG+4AjP5emkz345gV29Zjmi9Z5tYeg@mail.gmail.com
Whole thread Raw
In response to Re: 2nd attempt: Window function SQL - can't quite figure it.  (Pál Teleki <ellenallhatatlan@gmail.com>)
Responses Re: 2nd attempt: Window function SQL - can't quite figure it.
List pgsql-novice
On Mon, Jul 18, 2016 at 2:29 PM, Pál Teleki <ellenallhatatlan@gmail.com> wrote:

SELECT department,
       amount,
       salesmanager,
       -- DISTINCT(salesmanager), I tried using this line also...
       SUM(amount) OVER(PARTITION BY department, salesmanager) AS "Sum
by dept.",
       ROW_NUMBER() OVER() AS "row number",
       ROUND((amount/SUM(amount) OVER(PARTITION BY department,
salesmanager)) * 100) AS "% of total"
FROM sales
-- GROUP BY department, salesmanager
ORDER BY department, salesmanager

​Not Test.

You just want these rows returned, right?​

SELECT department, salesmanager
FROM sales
GROUP BY department, salesmanager

​Along with some calculations:

SELECT *, total_mgr_dept / total_dept_only AS manager_share_of_department
FROM (
SELECT department, salesmanager,
sum(amount)::numeric AS total_mgr_dept,
(sum(  sum(amount)  ) OVER (PARTITION BY department))::numeric AS total_dept_only,
FROM sales
GROUP BY department, salesmanager
​) group_sums

As I said in my other reply writing:

sum(amount) OVER ([...]) 

will not work.

David J.

pgsql-novice by date:

Previous
From: Pál Teleki
Date:
Subject: Re: 2nd attempt: Window function SQL - can't quite figure it.
Next
From: Pál Teleki
Date:
Subject: Re: 2nd attempt: Window function SQL - can't quite figure it.