I want to subtracted 23000 with 1000 again and again until its 0. So i want the result like this
1000 | 23000
1000 | 22000
1000 | 21000
1000 | 20000
.
.
.
1000 | 0
Can anyone help me how to do it in PostgreSQL? Thanks!
CodePudding user response:
You can generate a series
SELECT GENERATE_SERIES(23000 ,0 ,-1000)
| generate_series | | --------------: | | 23000 | | 22000 | | 21000 | | 20000 | | 19000 | | 18000 | | 17000 | | 16000 | | 15000 | | 14000 | | 13000 | | 12000 | | 11000 | | 10000 | | 9000 | | 8000 | | 7000 | | 6000 | | 5000 | | 4000 | | 3000 | | 2000 | | 1000 | | 0 |
db<>fiddle here
CodePudding user response:
using while loop
CREATE TABLE test(a int,x int);
do $$
declare
x1 integer := 23000;
begin
while x1 >= 0 loop
insert into test(a,x)
select 1000,x1 ;
x1 :=x1-1000;
end loop;
end$$;
select * from test