Moving thread over to SQL list as it belongs
there.
Bronx: This certainly is possible, but IMO,
not in one query. Actually doing it will be relatively complex. For
purposes of maintenance, I am thinking that doing this would be better handled
by wrapping at least one view.
CREATE VIEW sales_pre_proc AS
SELECT name, quantity, to_char("date", 'YYYY') AS
year, to_char("date", 'MM') FROM sales;
This is needed for the group by statement
below to function properly:
CREATE VIEW sales_month_summary AS
SELECT name, sum(quantity) AS quantity, year, month
from sales_pre_proc
GROUP BY name, year, month;
This will give you a view that will have the
sum information. Now we just have to create the statement which will
create the pivot effect. I understand that there is something under
contrib/tablefunc for this, but I do not have it on my system (cygwin), at
the moment. Perhaps someone else can help.
Failing that, you can write your own function to
return each row. I was working on a quick proof of concept but it was not
working properly.
Best Wishes,
Chris Travers
|
- Re: [SQL] Is it possible in PostgreSQL? Chris Travers
- Re: [SQL] Is it possible in PostgreSQL? Jim Johannsen