From: M. D. <lists@turnkey.bz>
To: pgsql-sql@postgresql.org
Subject: running totals with end of month line
Date: Wed, 30 Nov 2011 19:03:29 -0600
Message-ID: <4ED6D261.4060202@turnkey.bz> (raw)
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Hi everyone,<br>
<br>
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.<br>
<br>
Here's a sample query:<br>
<pre><code><span class="syntax0"><span class="syntax8">SELECT</span> n.customer_id, n.order_id, n.order_total,
<span class="syntax9">COALESCE</span><span class="syntax18">(</span><span class="syntax9">SUM</span><span class="syntax18">(</span>o.order_total<span class="syntax18">)</span>,<span class="syntax5">0</span><span class="syntax18">)</span> <span class="syntax8">As</span> past_order_total
<span class="syntax8">FROM</span> orders <span class="syntax8">AS</span> n <span class="syntax9">LEFT</span> <span class="syntax8">JOIN</span> orders <span class="syntax8">AS</span> o
<span class="syntax8">ON</span> <span class="syntax18">(</span>o.customer_id <span class="syntax18">=</span> n.customer_id
<span class="syntax8">AND</span> n.order_datetime <span class="syntax18">></span> o.order_datetime<span class="syntax18">)</span>
<span class="syntax8">GROUP</span> <span class="syntax8">BY</span> n.customer_id, n.order_datetime, n.order_id, n.order_total
<span class="syntax8">ORDER</span> <span class="syntax8">BY</span> n.customer_id, n.order_datetime, n.order_id;
--- taken from <a class="moz-txt-link-freetext" href="http://bit.ly/speZzs">http://bit.ly/speZzs</a;
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
</span></code></pre>
</body>
</html>
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: lists@turnkey.bz
Subject: Re: running totals with end of month line
In-Reply-To: <4ED6D261.4060202@turnkey.bz>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox