Y
Y
Ytsu Ytsuevich2017-03-04 13:16:47
Oracle
Ytsu Ytsuevich, 2017-03-04 13:16:47

SQL query increment inside group?

Not a newbie, like, but this is the first time I've come across this.
The following data is given:

num | txt
1 | q
1 | w
1 | e
2 | a
2 | s
2 | d
3 | z
3 | x

At the output you need to get:
num | txt
1 | q
2 | w
3 | e
1 | a
2 | s
3 | d
1 | z
2 | x

That is, increment data within the same group by num , but don't group them!
I draw your attention, the second column (and others except the first) should not change.
I would like to get a universal sql query, but since I write on oracle, the special case for it will descend also.
UPD
Here is the data for an example, in my case there are more columns, there is also an ID

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrew, 2017-03-04
@kofon

select num, txt, row_number() over (partition by num order by num) as rn from tab;

The only thing is that the order of numbering from your assignment is not clear.

K
Konstantin Tsvetkov, 2017-03-04
@tsklab

I would like to get a universal sql query
You don't have a key. If you use num+txt for it, then when you change it, you will get either a change prohibition (error) or an indeterminate record (error).
ID is also present
Build a cursor. Define a variable and start counting rows in it from 1 until num changes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question