假设我有两张桌子(顾客和发票)。我想了解最新的客户,根据他们的第一个发票日期x月。例如,一位顾客可能在11月份加入/注册,但他有可能在1月份下了第一次订单。所以他是我的新客户。样本表如下:

期望的结果:下面的只是上面示例数据的一个例子。C5的客户在2019年8月加入,但他在2020年1月下了第一个订单。所以对我来说,他是个新客户。

发布于 2020-01-27 15:31:36
如果要筛选在给定日期之后下第一次订单的客户,只需使用聚合:
select customer, min(date) first_invoice_date
from invoices
group by customer
having first_invoice_date > @somedate您可以根据需要调整(或删除) having子句。
如果您想了解客户的详细信息(如名称和国家),那么您可以加入customers表:
select c.*, o.first_invoice_date
from customers c
inner join (
select customer, min(date) first_invoice_date
from invoices
group by customer
having first_invoice_date > @somedate
) i on i.customer = c.idhttps://stackoverflow.com/questions/59934155
复制相似问题