我有一个SQL表,该表包含三列:customer_id,他们的第一笔订单的日期和他们的最后一张订单的日期。我正在尝试创建一个报告,以显示过去30天内重新订购的现有客户的百分比...即如果客户的第一笔订单在30天之前,而最后一笔订单在最近30天之内,则他们将计算在分子中。如果客户的第一笔订单在30天之前,那么他们将计入分母。
我希望输出只有两列:1)日期按时间顺序逆转; 2)符合该日期以上条件的客户百分比。我似乎无法弄清楚如何生成此报告。
有人知道如何开始吗?
答案 0 :(得分:0)
您可以为此使用lag()
。
select avg(case when last_order_date >= dateadd(day, -30, current_date) then 1.0 else 0 end)
from t
where first_order_date < dateadd(day, -30, current_date);
答案 1 :(得分:0)
很酷,所以它是一个“从一个地方获取两个结果,然后合并结果”的查询吗?如果您可以保证一个具有唯一的customer_id的客户表,这应该可以工作。我认为一天中总计的百分比方法比分子/分母更好,因为不会有“新”客户为0的日子和“老”客户为零的日子吗?
我也不太清楚,更重要的是新客户还是旧客户?这样可以提供一定比例的新客户,您可以根据需要切换显示旧客户的比例。
------- just some fake data - you won't need this because you have the real thing
WITH CUSTOMER_TABLE AS (
SELECT 1 AS customer_id , CAST('2017-08-01' AS DATE) AS first_order_date , CAST('2018-07-05' AS DATE) AS last_order_date UNION
SELECT 2 , '2018-07-01' , '2018-07-06' UNION
SELECT 3 , '2018-07-01' , '2018-07-07' UNION
SELECT 4 , '2018-07-01' , '2018-07-08' UNION
SELECT 5 , '2018-06-01' , '2018-07-09' UNION
SELECT 6 , '2018-05-31' , '2018-07-10' UNION
SELECT 7 , '2018-07-01' , '2018-07-11' UNION
SELECT 8 , '2018-07-01' , '2018-07-12' UNION
SELECT 9 , '2018-07-01' , '2018-07-13' UNION
SELECT 10 , '2018-07-01' , '2018-07-14' UNION
SELECT 11 , '2018-07-01' , '2018-07-15' UNION
SELECT 12 , '2018-03-04' , '2018-07-16' UNION
SELECT 13 , '2018-01-01' , '2018-07-17' UNION
SELECT 14 , '2018-07-01' , '2018-07-17' UNION
SELECT 15 , '2018-07-01' , '2018-07-17' UNION
SELECT 16 , '2018-01-01' , '2018-07-18' UNION
SELECT 17 , '2018-02-01' , '2018-07-18' UNION
SELECT 18 , '2018-07-02' , '2018-07-18' UNION
SELECT 19 , '2018-07-01' , '2018-07-18'
)
SELECT last_order_date ,
------ note the "1.00" forcing PostgreSQL to return a decimal result
1.00 * (count_of_new_customers)/(count_of_old_customers + count_of_new_customers ) AS percentage_of_new_customers ,
count_of_old_customers , count_of_new_customers ,
count_of_old_customers + count_of_new_customers AS total_customers_for_the_day
FROM
(
SELECT last_order_date ,
CAST(COUNT (CASE WHEN DATEDIFF(DAY, first_order_date , last_order_date) > 30 THEN 'old customer' END ) AS integer) AS count_of_old_customers ,
CAST(COUNT (CASE WHEN DATEDIFF(DAY, first_order_date , last_order_date) <= 30 THEN 'new customer' END ) AS integer) AS count_of_new_customers
----replace CUSTOMER_TABLE with your actual schema.table
FROM CUSTOMER_TABLE
GROUP BY last_order_date
)
----- here is the descending last_order_date you wanted
ORDER BY last_order_date DESC