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

From Pál Teleki
Subject Re: 2nd attempt: Window function SQL - can't quite figure it.
Date
Msg-id CAMLfE0Pjfze5r-iWo_bZAGMq6ELqVOtpf6QuM-eo84qd9SuEyQ@mail.gmail.com
Whole thread Raw
In response to Re: 2nd attempt: Window function SQL - can't quite figure it.  ("David G. Johnston" <david.g.johnston@gmail.com>)
Responses Re: 2nd attempt: Window function SQL - can't quite figure it.
List pgsql-novice
Thanks for that - I managed

> SELECT *, total_mgr_dept / total_dept_only AS manager_share_of_department
> <rest of query snipped...>

I managed to do using two CTEs as shown below.

I was wondering, how would it be possible to do what I want
using traditional SQL? That is, no CTE's and no Windowing
functions?

====== My effort using two CTE's ====

WITH tab1 AS (
  SELECT department, salesmanager, SUM(amount) as sm1 FROM sales
  GROUP BY department, salesmanager
),
tab2 AS (
  SELECT department, SUM(amount) as sm2 FROM sales
  GROUP BY department
)
SELECT tab1.*, tab2.sm2, ROUND((tab1.sm1/tab2.sm2) * 100, 2) AS  "%age
of Dept. income"
FROM tab1
INNER JOIN tab2 ON
tab1.department = tab2.department;

> David J.


Pál


--

Pál Teleki


pgsql-novice by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: 2nd attempt: Window function SQL - can't quite figure it.
Next
From: "David G. Johnston"
Date:
Subject: Re: 2nd attempt: Window function SQL - can't quite figure it.