Skip to content
Advertisement

SQL BigQuery – how can I calculate for each order the the count of that customers’ orders in the preceding full 12 month period

For each order record I would like to calculate the count of prior orders, from that customer, over the previous full 12 month period (excluding the month of the order).

I can get the count work without the date limits (code below).

But I just can’t figure out how to limit the count to the ‘rolling’ date range.

I’d really appreciate any suggestions on what I’m missing!

This generates the following output (with comments on expected values).

Any suggestions much appreciated…

Advertisement

Answer

Consider below approach

if applied to sample data in your question – output is

enter image description here

To address you second requirement with null – use below

with output

enter image description here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement