【问题标题】:SQL bigquery: Bool column for previous monthSQL bigquery:上个月的布尔列
【发布时间】:2021-08-04 21:20:28
【问题描述】:

我需要在 sql 中为 id_customers 写一个 bool 列:

If there is a purchase this month and if there is a purchase in the past, then 1. 
If there is a purchase this month, but not in the past, then 0. 

First frame:
id_customers | sale_date

Total frame:
id_customers | sale_date | bool

感谢您的提前

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。样本数据和期望的结果将有助于确保您的问题是明确的。
  • 数据库未打开
  • 请尝试一下,展示您所做的事情以及任何特定的问题/问题,否则您似乎希望有人来做您的工作。 SO 不是免费的编码服务。

标签: sql google-bigquery


【解决方案1】:

您似乎想要每个客户的布尔值,以确定本月的购买是否是客户购买的第一个月。应该是这样的:

select t.*,
       (case when min(sale_date) < date_trunc('month', current_date)
             then 1 else 0
        end) as is_repeat_customer
from t
group by customer
having max(sale_date) >= date_trunc('month', current_date);

这使用 Postgres 语法,但这是任意的,因为您没有指定数据库。

【讨论】:

  • 这不是我所需要的。如果我在这个月有购买,我应该检查上个月
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
  • 2016-09-16
  • 2017-05-06
  • 2018-04-12
相关资源
最近更新 更多