I have a table A with below columns
Name age phoneNumber
A 26 12345
B 34 87654
C 5 98765
my output table also contains same column names with no data
I need to generate output table like below mentioned
Name age phoneNumber
A 10 12345
A 10 12345
A 6 12345
B 10 87654
B 10 87654
B 10 87654
B 4 87654
C 5 98765
that is everytime I will substract age with 10 until age < 10 if in input table, if the age is less that 10 then move that row as it is to output table
Can anyone please guide me how to do this operation to get desired output
This looks like a good spot to use a recursive cte:
with cte as (
select name, age, age % 10 new_age, phoneNumber, 1 i from mytable
union all
select name, age, 10, phoneNumber, i + 1
from cte
where age > i * 10
)
select name, new_age age, phoneNumber from cte order by name, age desc
name | age | phoneNumber :--- | --: | ----------: A | 10 | 12345 A | 10 | 12345 A | 6 | 12345 B | 10 | 87654 B | 10 | 87654 B | 10 | 87654 B | 4 | 87654 C | 5 | 98765
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.