Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XwB8H-0002LX-Rj for pgsql-sql@arkaria.postgresql.org; Wed, 03 Dec 2014 14:42:21 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XwB8H-0000u2-Ck for pgsql-sql@arkaria.postgresql.org; Wed, 03 Dec 2014 14:42:21 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XwB8G-0000tw-Hd for pgsql-sql@postgresql.org; Wed, 03 Dec 2014 14:42:20 +0000 Received: from mwork.nabble.com ([162.253.133.43]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XwB8D-0000KL-FM for pgsql-sql@postgresql.org; Wed, 03 Dec 2014 14:42:18 +0000 Received: from msam.nabble.com (unknown [162.253.133.85]) by mwork.nabble.com (Postfix) with ESMTP id BFFFAC72F46 for ; Wed, 3 Dec 2014 06:42:17 -0800 (PST) Date: Wed, 3 Dec 2014 07:42:16 -0700 (MST) From: Ron256 To: pgsql-sql@postgresql.org Message-ID: <1417617736671-5829086.post@n5.nabble.com> In-Reply-To: <1417031476188-5828414.post@n5.nabble.com> References: <1417012543637-5828331.post@n5.nabble.com> <1417023477345-5828381.post@n5.nabble.com> <1417024519548-5828385.post@n5.nabble.com> <1417025960949-5828390.post@n5.nabble.com> <1417026089049-5828392.post@n5.nabble.com> <1417026875109-5828394.post@n5.nabble.com> <1417029980347-5828407.post@n5.nabble.com> <1417031476188-5828414.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-Pg-Spam-Score: -0.3 (/) 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 I have modified my query but I am really wondering why I am getting incorrect results. Please see the following link. http://sqlfiddle.com/#!15/5897e/4. I am getting the same values for both Average 1 year spend and Average 6 months spend which might not be right. Explaining further, the CTE in the demo generates the first time orders of a customer which I exclude in the join when calculating the the Average Six months spend per year. WITH first_cust_cte AS ( SELECT o_1.persistent_key_str, min(o_1.ord_submitted_date) AS ord_date FROM orders o_1 GROUP BY o_1.persistent_key_str ), first_time_customer_orders_to_be_excluded_cte as ( SELECT o.persistent_key_str, o.ord_id FROM orders o JOIN first_cust_cte c ON o.persistent_key_str = c.persistent_key_str AND o.ord_submitted_date = c.ord_date ) -- 1 row per year SELECT EXTRACT(YEAR FROM ord_submitted_date) AS ordered , AVG(o.item_extended_actual_price_amt)::numeric(18,2) "Avg_6_months_spend" FROM (SELECT generate_series(min(ord_submitted_date) -- single query ... , max(ord_submitted_date) -- ... to get min / max , '1d')::date FROM orders) g (ord_submitted_date) LEFT join orders o USING (ord_submitted_date) LEFT JOIN first_time_customer_orders_to_be_excluded_cte c USING(persistent_key_str) WHERE o.ord_submitted_date >= g.ord_submitted_date - interval '6 MONTHS' AND ord_submitted_date <= g.ord_submitted_date + interval '6 MONTHS' AND c.ord_id <> o.ord_id GROUP BY 1 ORDER BY 1 Can someone help me out? I know someone out there has a solution. -- View this message in context: http://postgresql.nabble.com/generating-the-average-6-months-spend-excluding-first-orders-tp5828253p5829086.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