BigQuery计算timeperiod Group By之间的实例

时间:2016-03-04 16:40:18

标签: google-bigquery

我有一个上传到BigQuery的订单表,其中包含以下标题

  

ConsumerID,TransactionDate,Revenue,OrderID

ConsumerID& OrderID是整数 TransactionDate是TIMESTAMP

数据结构如下

ConsumerId   || TransactionDate          || Revenue   ||  OrderID
1            || 2014-10-27 00:00:00 UTC  || 55        ||  653745
1            || 2015-02-27 00:00:00 UTC  || 65        ||  767833
1            || 2015-12-27 00:00:00 UTC  || 456       ||  5676324
2            || 2014-10-27 00:00:00 UTC  || 56        ||  435261
2            || 2016-02-27 00:00:00 UTC  || 43        ||  5632436724

所以我的预期输出是

ConsumerId   || Count Of Orders In Last 12 months
    1        || 2
    2        || 1

我想计算一下客户在首次订购之日起的头12个月内的订单数量。

在大查询中,我写了以下内容

SELECT
  ConsumerId,
  COUNT(OrderNumber BETWEEN MIN(TransactionDate)AND DATE_ADD(MIN(TransactionDate),11,"MONTH")) AS CountOfOrdersTwelve,
FROM
  [ordertable.orders]
GROUP BY
  1,
  2
ORDER BY
  ConsumerId ;

但是这会出现以下错误

错误:(L3:157):无法按聚合分组。

有没有人知道这可以在bigquery中完成?

1 个答案:

答案 0 :(得分:2)

快速选项供您考虑(假设输入如下)

      (SELECT 1 AS ConsumerID, '2014-01-01' AS TransactionDate, 1 AS OrderID),
      (SELECT 1 AS ConsumerID, '2014-05-01' AS TransactionDate, 2 AS OrderID),
      (SELECT 1 AS ConsumerID, '2015-01-01' AS TransactionDate, 3 AS OrderID),
      (SELECT 1 AS ConsumerID, '2015-03-01' AS TransactionDate, 4 AS OrderID),
      (SELECT 1 AS ConsumerID, '2015-04-01' AS TransactionDate, 5 AS OrderID),
      (SELECT 1 AS ConsumerID, '2015-05-01' AS TransactionDate, 6 AS OrderID),

      (SELECT 2 AS ConsumerID, '2015-01-01' AS TransactionDate, 1 AS OrderID),
      (SELECT 2 AS ConsumerID, '2015-01-01' AS TransactionDate, 2 AS OrderID),
      (SELECT 2 AS ConsumerID, '2015-01-01' AS TransactionDate, 3 AS OrderID),
      (SELECT 2 AS ConsumerID, '2015-03-01' AS TransactionDate, 4 AS OrderID),
      (SELECT 2 AS ConsumerID, '2015-04-01' AS TransactionDate, 5 AS OrderID),
      (SELECT 2 AS ConsumerID, '2016-05-01' AS TransactionDate, 6 AS OrderID),

      (SELECT 3 AS ConsumerID, '2015-04-01' AS TransactionDate, 1 AS OrderID),
      (SELECT 3 AS ConsumerID, '2015-05-01' AS TransactionDate, 2 AS OrderID)

您的数据可能因数据类型而异,因此您需要相应调整

SELECT ConsumerID, MAX(CountOfOrders) AS CountOfOrdersTwelve
FROM (
  SELECT ConsumerID, CountOfOrders
  FROM (
    SELECT
      ConsumerID, TransactionDate,
      COUNT(1) OVER(PARTITION BY ConsumerID ORDER BY TransactionDate) AS CountOfOrders,
      FIRST_VALUE(TransactionDate) 
        OVER(PARTITION BY ConsumerID ORDER BY TransactionDate) AS firstTransactionDate
    FROM [ordertable.orders]
  ) HAVING DATEDIFF(TransactionDate, firstTransactionDate) <= 365
) GROUP BY ConsumerID ORDER BY ConsumerID
  

精简版

注意:此版本适用于STRING(如上述第一个解决方案的示例)和TIMESTAMP(如更新后的问题中){{1}的数据类型}

TransactionDate