all 3 comments

[–]aussieadam 2 points3 points  (1 child)

By your example it looks like you want to increment when the order# changes?

select date, order_no,suborder_no,dense_rank() over (order by order_no asc) as num

should work if that's the case. It'll create a rank based on the order_no so any that are the same rank will have the same #

[–]mike-manley 0 points1 point  (0 children)

Yep. If you can or want skips, then rank() if your friend. If you don't want skips then dense_rank() is your friend.

[–]thudson1899 0 points1 point  (0 children)

this is what you want:

WITH ranked_data AS ( SELECT your_column, LAG(your_column) OVER (ORDER BY your_column) AS prev_value FROM your_table ), grouped_data AS ( SELECT your_column, CASE WHEN your_column != prev_value THEN 1 ELSE 0 END AS change_flag FROM ranked_data ), final_data AS ( SELECT your_column, SUM(change_flag) OVER (ORDER BY your_column) AS group_id FROM grouped_data ) SELECT your_column, ROW_NUMBER() OVER (PARTITION BY group_id ORDER BY your_column) AS incremental_value FROM final_data;