Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XtTfi-0004CB-SS for pgsql-sql@arkaria.postgresql.org; Wed, 26 Nov 2014 03:53:42 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XtTfh-000458-R7 for pgsql-sql@arkaria.postgresql.org; Wed, 26 Nov 2014 03:53:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XtTfg-000451-Qz for pgsql-sql@postgresql.org; Wed, 26 Nov 2014 03:53:40 +0000 Received: from [162.253.133.43] (helo=mwork.nabble.com) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XtTfc-0003yP-1f for pgsql-sql@postgresql.org; Wed, 26 Nov 2014 03:53:39 +0000 Received: from msam.nabble.com (unknown [162.253.133.85]) by mwork.nabble.com (Postfix) with ESMTP id C399AB4A764 for ; Tue, 25 Nov 2014 19:53:34 -0800 (PST) Date: Tue, 25 Nov 2014 20:53:33 -0700 (MST) From: David G Johnston To: pgsql-sql@postgresql.org Message-ID: <1416974013524-5828256.post@n5.nabble.com> In-Reply-To: <1416970805198-5828253.post@n5.nabble.com> References: <1416970805198-5828253.post@n5.nabble.com> Subject: Re: generating the average 6 months spend excluding first orders MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 162.253.133.43 (failed) X-Pg-Spam-Score: 0.5 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Ron256 wrote > Hi all, > > I have to two tasks where I am supposed to generate the average 6 months > spend and average 1 year spend using the customer data but excluding the > first time orders. > > SELECT q.ord_year, avg( item_extended_actual_price_amt ) > [...] > GROUP BY q.ord_year > ORDER BY q.ord_year > ; > > Can someone help me look into my query and see whether I am doing it the > right way before I go a head to do the same for the average 1 year spend? > > Any suggestions are highly appreciated. You do not specify whether you want rolling or calendar periods. The query group by forces calendar year boundaries but I would typically think that TTM (trailing-twelve-months) and TSM values would be more appropriate. If you are going to execute the query often it would likely be worthwhile to identify the entity for "first order" (i.e., buyer) as a separate table and simply store the orderID of their first order in the table. Your query can then simply pull all transactions from the past 6 or 12 months, join against the buyer, and omit any record that matches the first orderid stored on the buyer table. David J. -- View this message in context: http://postgresql.nabble.com/generating-the-average-6-months-spend-excluding-first-orders-tp5828253p5828256.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql