Sql 带条件BIGQUERY的行号()

Sql 带条件BIGQUERY的行号(),sql,google-bigquery,Sql,Google Bigquery,我真的很感激你能帮我。 我有一套购买旅游的数据。每个巡演都有一封买家电子邮件和活动日期以及更多其他不相关的栏目。 我希望有一个列trip来确定该事件是新的trip还是相同的trip。 要将新购买确定为新旅行,两个活动日期之间的差异必须超过30天。如果不是,那次旅行被认为是同一次旅行。最后,我需要知道客户做了多少次旅行,并按旅行对购买进行分组。 我使用行编号()进行查询,并计算第一次购买和下一次购买之间的日期差异。我觉得我很接近,但是我需要一些帮助来添加Trip列 我需要这样的东西: 此文件中包

我真的很感激你能帮我。 我有一套购买旅游的数据。每个巡演都有一封买家电子邮件和活动日期以及更多其他不相关的栏目。 我希望有一个列trip来确定该事件是新的trip还是相同的trip。 要将新购买确定为新旅行,两个活动日期之间的差异必须超过30天。如果不是,那次旅行被认为是同一次旅行。最后,我需要知道客户做了多少次旅行,并按旅行对购买进行分组。 我使用
行编号()
进行查询,并计算第一次购买和下一次购买之间的日期差异。我觉得我很接近,但是我需要一些帮助来添加Trip列

我需要这样的东西:

此文件中包含示例数据集和我需要的列: 原始数据是第一个选项卡, 下面的查询结果位于第二个选项卡中,列为橙色,最后一列为红色,这就是我要查找的列

WITH NumberedDates AS (
SELECT
City
,Booking
,Purchase_Date
, Purchaser_Email
,Guest_Info
,Addr_1
,City_7
,State_Province
,Country
, Gross_Sales
, Event_Date
, Event_Name
, MIN(Event_Date) OVER (PARTITION BY Purchaser_Email) as minPurchDate
, ROW_NUMBER() OVER (PARTITION BY Purchaser_Email ORDER BY Event_Date) AS RowNo
FROM SalesEatingEurope.DymTable )



SELECT
n1.City
, n1.Booking
, n1.Purchase_Date
, n1.Purchaser_Email
, n1.Guest_Info
, n1.Addr_1
, n1.City_7
, n1.State_Province
, n1.Country
, n1.Gross_Sales
, n1.Event_Name
, n1.Event_Date
, n1.RowNo as TransactionNumber
, n2.Event_Date as PrevEventDate
, IFNULL(date_diff(EXTRACT(DATE FROM n2.Event_Date), EXTRACT(DATE FROM n1.Event_Date) ,day), 0)*-1 AS DaysSincePrevEvent
, n1.minPurchDate as FirstEvent
, IFNULL(date_diff( EXTRACT(DATE FROM n1.minPurchDate), EXTRACT(DATE FROM n1.Event_Date) ,day), 0)*-1 AS DaysSinceFirstEvent
FROM NumberedDates  AS n1
LEFT JOIN NumberedDates  AS n2
ON n1.Purchaser_Email = n2.Purchaser_Email
AND n1.RowNo = n2.RowNo + 1
ORDER BY n1.Purchaser_Email, n1.Event_Date

你走得很对。在划分和分配
行号()
秩()
后,可以根据两次购买滞后于特定增量的情况分配布尔参数

以下是实现这一目标的方法:

with data as (
  select purchaser_email, event_date, rank() over (partition by purchaser_email order by event_date) as indx from (
    select 'abc_xyz@xyz.com' as purchaser_email, date('2018-10-15') as event_date union all
    select 'abc_xyz@xyz.com' as purchaser_email, date('2018-10-12') as event_date union all
    select 'abc_xyz@xyz.com' as purchaser_email, date('2018-10-19') as event_date union all
    select 'fgh_xyz@xyz.com' as purchaser_email, date('2018-10-03') as event_date union all
    select 'fgh_xyz@xyz.com' as purchaser_email, date('2018-10-10') as event_date union all
    select 'fgh_xyz@xyz.com' as purchaser_email, date('2018-11-26') as event_date union all
    select 'abc_xyz@xyz.com' as purchaser_email, date('2018-11-28') as event_date union all
    select 'abc_xyz@xyz.com' as purchaser_email, date('2018-12-30') as event_date union all
    select 'abc_xyz@xyz.com' as purchaser_email, date('2018-12-31') as event_date
  )
)
select purchaser_email, count(1) as order_count from (
  select purchaser_email, 
    d1, new_purchase, sum(case when new_purchase=true then 1 else 0 end) over (partition by purchaser_email order by d1) as purchase_count from (
    select 
      t1.purchaser_email, 
      t1.event_date as d1, 
      t2.event_date as d2, 
      t1.indx as t1i,
      t2.indx as t2i,
      case 
        when t2.event_date is null then true 
        when abs(date_diff(t1.event_date, t2.event_date, day)) >= 30 then true 
        else false end as new_purchase
      from data t1
      left join data t2 on t1.purchaser_email = t2.purchaser_email and t1.indx-1 = t2.indx
  )
  order by 1,2,3
)
where new_purchase = true
group by 1
order by 1

嗨,汗,谢谢你的回复。我需要更多的帮助,因为我知道你在做什么,但我需要一列更多的旅行号码。也许我需要的是一个光标。我不知道怎么处理排号。请看一下我想要的输出。我希望你能帮助我。非常感谢阿加尼更新了答案。如果有帮助,请将其标记为正确答案。谢谢