Google bigquery 确定时间差的平均值

Google bigquery 确定时间差的平均值,google-bigquery,Google Bigquery,我有一个名为customertransactions的表,列为:CustomerID,TransactionDate(类型:timestamp) 我想计算客户交易时间之间的时间差的平均值 我知道在熊猫身上可以做到这一点 df[['CustomerID','TransactionDate']].drop_duplicates().groupby('CustomerID')['TransactionDate']\ .apply(lambda x

我有一个名为customertransactions的表,列为:CustomerID,TransactionDate(类型:timestamp)

我想计算客户交易时间之间的时间差的平均值

我知道在熊猫身上可以做到这一点

df[['CustomerID','TransactionDate']].drop_duplicates().groupby('CustomerID')['TransactionDate']\
                            .apply(lambda x: np.mean([i / np.timedelta64(1, 'D') for i in np.diff([pd.to_datetime(c) for c in x])[0]]))\
                            .reset_index(name='MEAN_TIME_BETWEEN_VISITS')
任何帮助都将不胜感激

WITH CalculatedDifference AS (
  SELECT 
    CustomerID,
    TIMESTAMP_DIFF(
      TransactionDate,
      LAG(TransactionDate) OVER (PARTITION BY CustomerID ORDER BY TransactionDate ASC),
      MINUTE
    ) as Difference
  FROM `your_dataset_id.customer`
)
SELECT CustomerID, AVG(Difference)
FROM CalculatedDifference
GROUP BY CustomerID