Assume I have the following table: http://sqlfiddle.com/#!17/3de963/1
What I am trying to calculate is for each paidmonth in the table, I want to calculate total users and total purchase_amt for that month and 11 months prior (so a total of 12 months, including the month of the current row).
I can calculate the total amount easily and I have done this by doing:
sum(purchase_amt) over (order by paidmonth asc rows 11 preceding)
However, when I try to do:
count(distinct user_id) over (order by paidmonth asc rows 11 preceding)
I get this error:
Window ORDER BY is not allowed if DISTINCT is specified
So this is the result I am hoping to get:
| paidmonth | total_unique_users | total_amount |
| ---------- | ------------------ | ------------ |
| 2020-10-01 | 1 | 20 |
| 2020-11-01 | 1 | 50 |
| 2020-12-01 | 1 | 100 |
| 2021-06-01 | 2 | 180 |
| 2022-03-01 | 2 | 85 |
| 2022-06-01 | 1 | 105 |
| 2022-10-01 | 2 | 175 |
If there are any additional columns you require, please let me know and I will help. The table I have shown in the link is a summary CTE.