I have an order table uploaded to BigQuery that contains the following headers
ConsumerID, TransactionDate, Revenue, OrderID
ConsumerID & OrderID are Integers TransactionDate is a TIMESTAMP
The Data is Structured as Follows
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
So my expected output would be
ConsumerId || Count Of Orders In Last 12 months
1 || 2
2 || 1
I want to do a count of the number of orders a customer has placed in the 1st 12 months since the date of their first order.
In big query i have written the following
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 ;
However this errors with the following
Error: (L3:157): Cannot group by an aggregate.
Does anyone know a way this can be done in bigquery?