Skip to content
Advertisement

SQL Getting row number only when the value is different from all previous values

I want the count adding one only when the value has not been show before. The base table is:

The goal is:

I am thinking to compare the current row with all previous rows, but I have difficulty to call all previous rows. Thank you!

Advertisement

Answer

Several different ways to accomplish this. I guess you’ll get to pick one you like the best. This one just finds the first row number per product. You then just need to collapse the holes with an easy application of dense_rank() to the initial grouping.

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