Re: running totals with end of month line

Поиск
Список
Период
Сортировка
От David Johnston
Тема Re: running totals with end of month line
Дата
Msg-id E71E635E-4A47-46BF-8899-78D1837D6FBA@yahoo.com
обсуждение исходный текст
Ответ на running totals with end of month line  ("M. D." <lists@turnkey.bz>)
Список pgsql-sql
On Nov 30, 2011, at 20:03, "M. D." <lists@turnkey.bz> wrote:

Hi everyone,

I would like to create a query that does a running total for each account, but I also want to show a 'phantom' row that gives the end of month with the last day of the month as the transaction date.

Here's a sample query:
SELECT n.customer_id, n.order_id, n.order_total,     COALESCE(SUM(o.order_total),0) As past_order_total
FROM orders AS n LEFT JOIN orders AS o     ON (o.customer_id = n.customer_id             AND n.order_datetime > o.order_datetime)
GROUP BY n.customer_id, n.order_datetime, n.order_id, n.order_total
ORDER BY n.customer_id, n.order_datetime, n.order_id;

--- taken from http://bit.ly/speZzs

Is there a way to have that 'phantom' row for each account?  I want to result to be ordered by customer_id, account_type.

More details:
In my situation, I have Customers and Grain types.  
I want to generate a result that will show Customer, Grain Type, Daily Avg Bal, Charge Rate, discount, Charge.  
Maybe it's not really possible. I see it a bit hard group it properly, showing only single row per customer per grain.


Many thanks,
Mark
You need to write the totaling query and then UNION ALL it with the detail query.  You probably will want to use WINDOW aggregates as well - assuming you are on 8.4 or better.

David J.

В списке pgsql-sql по дате отправления:

Предыдущее
От: "M. D."
Дата:
Сообщение: running totals with end of month line
Следующее
От: Muiz
Дата:
Сообщение: plpgsql: how to get the exception's detail information?