split(...) style functions

From: Patrick Bakker <patrick(at)vanbelle(dot)com>
To: "PostgreSQL General (E-mail)" <pgsql-general(at)postgresql(dot)org>
Subject: split(...) style functions
Date: 2002-09-04 16:09:30
Message-ID: A9CE1D556F89DD4FBA4CF797215DF61A02F35C@20svbl1.vanbelle.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I'm looking for a function that can output the following:

Total Sales Product_0 Product_1 Product_2 Other
Jan 5000 1000 1000 1000 2000
Feb 6000 2000 500 500 3000
Mar 3000 1000 700 200 1100
...

Basically, I would like to split a column into horizontal groups using a
select/case/switch style statement.
It might look something like (even without the SUMSPLIT function I'm not
sure if the query is correct but maybe you'll get the idea...) the following
assuming sales order line table (so_line) has columns for order date
(orderdate), the price to be paid for that line (totalprice), and a
reference to a particular item (fk_item):

SELECT
to_char(datetime(so_line.orderdate),'MMM'),
SUM(so_line.totalprice),
SUMSPLIT(so_line.totalprice, so_line.fk_item = 0, so_line.fk_item = 1,
so_line.fk_item = 2, else)
FROM
so_line
WHERE
...
GROUP BY
extract(month from so_line.orderdate)
ORDER BY
extract(month from so_line.orderdate) ASC;

Essentially, I'd like to have SUMSPLIT defined as:
The first value is the column that will be summed.
All other arguments are the conditions for creating a new column in the
output (which will
obey grouping order by resetting the running sum totals for each split
column created).
'else' can be optionally specified to indicate that anything not matching
any of the conditions
will get dumped there.

OTHER POSSIBILITIES:
- Perhaps it should also take as an argument for each condition, the
column name to be displayed.
- If there is a SUMSPLIT then there should also be COUNTSPLIT, AVGSPLIT,
etc...

Does anybody else have any suggestions on how to accomplish a similar result
without creating a new function?
If not, I would appreciate any pointers and advice on how this could be
accomplished as I may start investigating how to actually implement these
functions.

Patrick

Browse pgsql-general by date

  From Date Subject
Next Message Mourad Dhambri 2002-09-04 16:31:04
Previous Message Michael Lam 2002-09-04 15:40:06 libpq question...