Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WJ2ai-00079i-Ff for pgsql-sql@arkaria.postgresql.org; Thu, 27 Feb 2014 15:09:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WJ2ah-0001cn-Pi for pgsql-sql@arkaria.postgresql.org; Thu, 27 Feb 2014 15:09:39 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WJ2ah-0001ch-1D for pgsql-sql@postgresql.org; Thu, 27 Feb 2014 15:09:39 +0000 Received: from sam.nabble.com ([216.139.236.26]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WJ2ae-0006xb-1F for pgsql-sql@postgresql.org; Thu, 27 Feb 2014 15:09:38 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1WJ2ac-0005wb-CV for pgsql-sql@postgresql.org; Thu, 27 Feb 2014 07:09:34 -0800 Date: Thu, 27 Feb 2014 07:09:34 -0800 (PST) From: David Johnston To: pgsql-sql@postgresql.org Message-ID: <1393513774376-5793882.post@n5.nabble.com> In-Reply-To: <1001132708.567696.1393510818245.JavaMail.root@mobiletech.sk> References: <1001132708.567696.1393510818245.JavaMail.root@mobiletech.sk> Subject: Re: how to effectively SELECT new "customers" MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 4.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 Jan Ostrochovsky wrote > Hello, I am solving following task and it seems hard to me to find > effective solution. Maybe somebody knows how to help me: We have table > "purchases" and each one record is identified by "customer_id". We want to > know not only how many different customers did at least one purchase per > time period, grouped by time periods (easy task: "COUNT(DISTINCT > customer_id)" with "GROUP BY period"), but also to know how many NEW > customers there were. We define new customer as customer_id, which had > first record in table "purchases" after 12 month of inactivity (no record > in table "purchases" previous 12 months). I have found one solution, but > it is very slow and ugly. I tried several other concepts, but without > success. Any hint could be helpful for me. Thanks in advance! Jano Without incorporating additional meta-data about the purchases onto the customer table the most basic solution would be: SELECT DISTINCT customer_id FROM products WHERE date > (now() - '12 months'::interval) EXCEPT SELECT DISTINCT customer_id FROM products WHERE date <= (now() - '12 months'::interval) --- Another solution: WHERE ... >12 AND NOT EXISTS (SELECT ... WHERE <= 12) --- Depending on the frequency that you need to run this query it may be worthwhile to create a materialized view that captures the necessary data and then whenever a new sale is generated you simply update that view by changing the attributes of that single customer. At any point you can quickly determine, using the view, which customers were active at the time of last purchases and which ones were dormant or non-existent. David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/how-to-effectively-SELECT-new-customers-tp5793867p5793882.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