I have the following table:
product price date
banana 90 2022-01-01
banana 90 2022-01-02
banana 90 2022-01-03
banana 95 2022-01-04
banana 90 2022-01-05
banana 90 2022-01-06
I need to add a non-unique ID column to the table. Every time the price changes, I want the ID to change. This would result in the following table.
id product price date
A banana 90 2022-01-01
A banana 90 2022-01-02
A banana 90 2022-01-03
B banana 95 2022-01-04
C banana 90 2022-01-05
C banana 90 2022-01-06
By searching for answers in SO and Google, I was able to create a column (my_seq) that contains a sequence that resets every time (see sql fiddle for my query) the price changes. But I still don’t know how to create an ID column that resets every time the my_seq starts over.
my_seq rn1 rn2 product price date
1 1 1 banana 90 2022-01-01
2 2 2 banana 90 2022-01-02
3 3 3 banana 90 2022-01-03
1 1 4 banana 95 2022-01-04
1 4 5 banana 90 2022-01-05
2 5 6 banana 90 2022-01-06
sql-fiddle with DDL and my query
thanks
3
Answers
You are half-there already with the query in your Fiddle.
Consider what you get if you subtract your rn1 & rn2 values – you get the values you need to group by.
If you want an increasing sequence you can then apply a dense rank:
Modified fiddle
It will generate sequence number whenever price change.
output:
lag()
window function references previous row within the same ordered group. It’snull
for the first row, hence thecoalesce()
later::int
, resulting in0
for when it’s unchanged and1
where it changed.sum() over (... groups between unbounded preceding and current row)
is just a rolling sum collecting everything so far, up to the current row in the window. Rolling over0
‘s and1
‘s it’ll grow only for rows where the price changed.chr(ascii(start_char) + offset)
can get you the alphabetical sequence, but it also limits it to alphabet length.Sample output from a demo with more tests: