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

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Pál Teleki <ellenallhatatlan(at)gmail(dot)com>
Cc: pgsql-novice novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: 2nd attempt: Window function SQL - can't quite figure it.
Date: 2016-07-18 19:00:33
Message-ID: CAKFQuwY+w2H028pw9ajghG+4AjP5emkz345gV29Zjmi9Z5tYeg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Mon, Jul 18, 2016 at 2:29 PM, Pál Teleki <ellenallhatatlan(at)gmail(dot)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.

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Pál Teleki 2016-07-18 19:24:20 Re: 2nd attempt: Window function SQL - can't quite figure it.
Previous Message Pál Teleki 2016-07-18 18:29:19 Re: 2nd attempt: Window function SQL - can't quite figure it.