Skip to content
Advertisement

Calculate order sequence in BigQuery

I am new to BigQuery SQL and struggling with calculating an “order sequence” from a table of customer orders.

In my example, I am working with customer purchases, where I have the sales channel, the unique order number, the order date and other purchase as well as customer information. I am also joining two tables in my query to get all the necessary data into one table.

This is how my current query looks like:

However, based on this query, I also want to add another column with the “order sequence”, which is calculated on a customer level and based on the purchase date.

Hence, the result should look like the following with the calculated order sequence:

How would I best integrate this in my query above without interfering with the JOIN and my filters?

Thank you!

Advertisement

Answer

If I understand correctly, you would use window functions:

Note that I changed the table aliases. Instead of using meaningless letters, these are abbreviations for the table names. That makes the query much simpler to follow.

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