Re: column totals - Mailing list pgsql-performance

From James Neethling
Subject Re: column totals
Date
Msg-id 44770077.8010506@silversphere.co.za
Whole thread Raw
In response to column totals  (James Neethling <james@silversphere.co.za>)
List pgsql-performance

James Neethling wrote:
> Hi There,
>
> I've got a situation where I need to pull profit information by
> product category, as well as the totals for each branch.
>
> Basically, something like
>
> SELECT branch_id, prod_cat_id, sum(prod_profit) as prod_cat_profit
>    FROM () as b1
> WHERE x = y
> GROUP BY branch, prod_cat_id
>
>
> Now, I also need the branch total, effectively,
> SELECT branch_id, sum(prod_profit) as branch_total
>    FROM () as b1
> WHERE x = y
> GROUP BY branch_id.
>
>
> Since the actual queries for generating prod_profit are non-trivial,
> how do I combine them to get the following select list?
SELECT branch_id, prod_cat_id, sum(prod_profit) as prod_cat_profit,
sum(prod_profit) as branch_total
>
> Or is there a more efficient way?
>
> Kind Regards,
> James
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
>                http://archives.postgresql.org
>

Attachment

pgsql-performance by date:

Previous
From: James Neethling
Date:
Subject: column totals
Next
From: Tom Lane
Date:
Subject: Re: is it possible to make this faster?