Skip to content
Advertisement

Update statement within select statement

I am trying to write a query in which I update a counter based on other conditions. For example:

Here’s the basic jist of what I’m trying to do. I want a table that looks like this:

box_type identifier
small 1
small 1
small 1
medium 2
medium 2
large 3
large 3
small 4

I just want to increment that identifier value every time the box_type changes

Thank you!

Advertisement

Answer

Your question only makes sense if you have a column that sepcifies the ordering. Let me assume such a column exists — based on your code, I’ll call it time.

Then, you can use lag() and a cumulative sum:

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