将 date_diff 天除以 365 得到年数

Divide date_diff in days with 365 to get number of years

当我意识到第 0 年的订单数量错误时,我在第一次购买和第二次购买之间进行了 DATE_DIFF。客户在 2020 年 10 月下了第一笔订单,第二笔订单在 2021 年 1 月结束对于 1 年后完成第二个订单的客户,将其放入垃圾箱 - 新订单的年份已更改,但订单之间只有几个月的时间。

我尝试用天数而不是年数来做一个 DATE_DIFF,并将它除以 365,但这给了我小数作为年数。下面是我在 DataStudio 中使用的查询,我在那里按 calc_yearsBetweenFirstTwoOrders 对订单数量进行分组。有人知道如何处理吗?

DATE_DIFF(a.casted_order_datetime, a.first_customer_purchase_date, YEAR) AS yearsBetweenFirstTwoOrders,
DATE_DIFF(a.casted_order_datetime, a.first_customer_purchase_date, DAY) / 365 AS calc_yearsBetweenFirstTwoOrders,
DATE_DIFF(a.casted_order_datetime, a.first_customer_purchase_date, DAY) AS daysBetweenFirstTwoOrders,
DATE_DIFF(a.casted_order_datetime, a.first_customer_purchase_date, WEEK) AS weeksBetweenFirstTwoOrders,
DATE_DIFF(a.casted_order_datetime, a.first_customer_purchase_date, MONTH) AS monthsBetweenFirstTwoOrders,

CASE WHEN (DATE_DIFF(a.casted_order_datetime, a.first_customer_purchase_date, DAY) = 0) THEN 'newCustomer' 
     WHEN (DATE_DIFF(a.casted_order_datetime, a.first_customer_purchase_date, DAY) > 0) THEN 'returningCustomer' 
ELSE NULL END AS customerSegment,

CASE WHEN (article_price BETWEEN 0 AND 1000) THEN 'price 0-1000'
     WHEN (article_price BETWEEN 1000 and 2000) THEN 'price 1000-2000'
     WHEN (article_price BETWEEN 2000 and 3000) THEN 'price 2000-3000'
     WHEN (article_price BETWEEN 3000 and 4000) THEN 'price 3000-4000'
     WHEN (article_price BETWEEN 4000 and 5000) THEN 'price 4000-5000'
     WHEN (article_price BETWEEN 5000 and 6000) THEN 'price 5000-6000'
ELSE 'price > 6000' END AS article_price_binned,


from (



SELECT 
DISTINCT order_id,
first_customer_purchase_date,
CAST(TIMESTAMP(order_datetime)as DATE) as casted_order_datetime,
order_datetime as order_datetime,
customer_id,
orderrow_id,
article_title,
main_image,
article_price,
category_level_1,
category_level_2,
category_level_3,
merchant,
traffic_source,
is_return,
Order_Product_Age_Days,
order_shipping_time_max,
Quantity,
  ROW_NUMBER() OVER ( PARTITION BY customer_id ORDER BY order_datetime desc ) AS RN
  
FROM `xxxx-xxxx-xxxx.sandbox.xxxxxx`

)A

WHERE order_datetime BETWEEN PARSE_TIMESTAMP('%Y%m%d', @DS_START_DATE) 
AND PARSE_TIMESTAMP('%Y%m%d', @DS_END_DATE)

当您将日期差异除以 365 以表示年份差异时,您可以从输出中提取整数。为此,您可以将查询调整为:

CAST(REGEXP_EXTRACT(CAST(DATE_DIFF(a.casted_order_datetime, a.first_customer_purchase_date, DAY)/365 AS STRING),r'^(\d+)\.\d+') AS INT64) AS calc_yearsBetweenFirstTwoOrders

注意:我将它转换回 INT64,这样它就不会被视为字符串。

我用这个例子测试了它:

SELECT
  CAST(REGEXP_EXTRACT(CAST(DATE_DIFF(DATE '2021-01-01', DATE '2020-10-01', DAY)/365 AS STRING),r'^(\d+)\.\d+') AS INT64) AS calc_yearsBetweenFirstTwoOrders

2021-01-01 和 2020-10-01:

2021-01-01 和 2020-01-01:

2021-01-01 和 2019-06-01: