Unix Technical Forum

column totals

This is a discussion on column totals within the Pgsql Performance forums, part of the PostgreSQL category; --> Hi There, I've got a situation where I need to pull profit information by product category, as well as ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql Performance

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 08:52 AM
James Neethling
 
Posts: n/a
Default column totals

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?

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 08:52 AM
James Neethling
 
Posts: n/a
Default Re: column totals



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
>



---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 08:52 AM
Ragnar
 
Posts: n/a
Default Re: column totals

On fös, 2006-05-26 at 11:56 +0200, James Neethling wrote:

> 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?


one simple way using temp table and 2 steps:

CREATE TEMP TABLE foo AS
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;

SELECT branch_id,
prod_cat_id,
prod_cat_profit,
branch_total
FROM foo as foo1
JOIN
(SELECT branch_id,
sum(prod_cat_profit) as branch_total
FROM foo
GROUP BY branch_id
) as foo2 USING branch_id;


(untested)

gnari



---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:27 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com