Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WJLrc-0000Cg-7R for pgsql-sql@arkaria.postgresql.org; Fri, 28 Feb 2014 11:44:24 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WJLrb-0007Xj-FS for pgsql-sql@arkaria.postgresql.org; Fri, 28 Feb 2014 11:44:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WJLra-0007Xc-EJ for pgsql-sql@postgresql.org; Fri, 28 Feb 2014 11:44:22 +0000 Received: from zimbra.isdd.sk ([91.233.248.204]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WJLrS-0004N2-HF for pgsql-sql@postgresql.org; Fri, 28 Feb 2014 11:44:21 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by zimbra.isdd.sk (Postfix) with ESMTP id 583A5AC86A7; Fri, 28 Feb 2014 12:44:13 +0100 (CET) X-Virus-Scanned: amavisd-new at zimbra.isdd.sk Received: from zimbra.isdd.sk ([127.0.0.1]) by localhost (zimbra.isdd.sk [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 5aPDS4bHeUPg; Fri, 28 Feb 2014 12:44:12 +0100 (CET) Received: from zimbra.isdd.sk (zimbra.isdd.sk [91.233.248.204]) by zimbra.isdd.sk (Postfix) with ESMTP id 84577AC865E; Fri, 28 Feb 2014 12:44:12 +0100 (CET) Date: Fri, 28 Feb 2014 12:44:12 +0100 (CET) From: Jan Ostrochovsky To: David Johnston Cc: pgsql-sql@postgresql.org Message-ID: <966514640.595308.1393587852428.JavaMail.root@mobiletech.sk> In-Reply-To: <1393513774376-5793882.post@n5.nabble.com> Subject: Re: how to effectively SELECT new "customers" MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_595307_1818397471.1393587852427" X-Originating-IP: [10.8.253.141] X-Mailer: Zimbra 7.2.6_GA_2926 (ZimbraWebClient - GC33 (Win)/7.2.6_GA_2926) X-Pg-Spam-Score: -1.9 (-) 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 ------=_Part_595307_1818397471.1393587852427 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit > From: "David Johnston" > To: pgsql-sql@postgresql.org > Sent: Thursday, February 27, 2014 4:09:34 PM > Subject: Re: [SQL] how to effectively SELECT new "customers" > 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. ------=_Part_595307_1818397471.1393587852427 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable <= div style=3D'font-family: times new roman,new york,times,serif; font-size: = 12pt; color: #000000'>
From: "David Johnston" <polobo@yahoo.com= >
To: pgsql-sql@postgresql.org
Sent: Thursday, Febru= ary 27, 2014 4:09:34 PM
Subject: Re: [SQL] how to effectively SEL= ECT new "customers"

Jan Ostrochovsky wrote
> Hello, I am solvi= ng following task and it seems hard to me to find
> effective solutio= n. Maybe somebody knows how to help me: We have table
> "purchases" a= nd 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
> c= ustomer_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 rec= ord
> in table "purchases" previous 12 months). I have found one solu= tion, but
> it is very slow and ugly. I tried several other concepts,= but without
> success. Any hint could be helpful for me. Thanks in a= dvance! Jano

Without incorporating additional meta-data about the pu= rchases 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 W= HERE date <=3D (now() - '12
months'::interval)

---

Anot= her solution:
WHERE ... >12 AND NOT EXISTS (SELECT ... WHERE <=3D = 12)

---

Depending on the frequency that you need to run this = query it may be
worthwhile to create a materialized view that captures t= he necessary data
and then whenever a new sale is generated you simply u= pdate that view by
changing the attributes of that single customer. &nbs= p;At any point you can
quickly determine, using the view, which customer= s were active at the time
of last purchases and which ones were dormant = or non-existent.

David J.

------=_Part_595307_1818397471.1393587852427--