Re: how to effectively SELECT new "customers"

Поиск
Список
Период
Сортировка
От David Johnston
Тема Re: how to effectively SELECT new "customers"
Дата
Msg-id 1393513774376-5793882.post@n5.nabble.com
обсуждение исходный текст
Ответ на how to effectively SELECT new "customers"  (Jan Ostrochovsky <ostrochovsky@mobiletech.sk>)
Ответы Re: how to effectively SELECT new "customers"  (Jan Ostrochovsky <ostrochovsky@mobiletech.sk>)
Re: how to effectively SELECT new "customers"  (Jan Ostrochovsky <ostrochovsky@mobiletech.sk>)
Re: how to effectively SELECT new "customers"  (Jan Ostrochovsky <ostrochovsky@mobiletech.sk>)
Список pgsql-sql
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.



В списке pgsql-sql по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Function Issue
Следующее
От: Jan Ostrochovsky
Дата:
Сообщение: Re: how to effectively SELECT new "customers"