Hi I'm trying to populate the row_number using row_number() over (partition, but the results is not giving me the row number 1 where the first record is cumulative_cost >= threshold and the subsequent rows all have the same row number.
This is the result
date | cost_amt | cumulative_cost | threshold_reached_order | product_id |
---|---|---|---|---|
9/07/2023 | 14.09 | 14.09 | 0 | 12345 |
9/07/2023 | 10.2 | 24.29 | 0 | 12345 |
9/07/2023 | 25.03 | 49.32 | 3 | 12345 |
11/07/2023 | 28.09 | 77.41 | 4 | 12345 |
I'm expecting this result
date | cost_amt | cumulative_cost | threshold_reached_order | product_id |
---|---|---|---|---|
9/07/2023 | 14.09 | 14.09 | 0 | 12345 |
9/07/2023 | 10.2 | 24.29 | 0 | 12345 |
9/07/2023 | 25.03 | 49.32 | 1 | 12345 |
11/07/2023 | 28.09 | 77.41 | 2 | 12345 |
select date, cost_amt, cumulative_cost,
case when cumulative_cost >= threshold then
row_number() OVER (partition BY product_name, userid ORDER BY date, order_id)
else 0
end AS threshold_reached_order,
product_id
from table1
where userid = 'ABCDEFG'
order by userid , product_id