I want to create a new column in my table. I want this new column to give the label "done" for every 5 rows.
My table looks like this:
no type 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 a
and what I mean with the new column is:
no type flag 1 a 2 a 3 a 4 a 5 a done 6 a 7 a 8 a 9 a 10 a done
so the "done" label will always appear for every 5 rows.
I use postgres for this. How can I do that with query?
Thanks in advance.
You can use the row_number() function and the modulo operator:
row_number()
select no, type, case when row_number() over (order by no) % 5 = 0 then 'done' end as flag from the_table order by no;
1.4m articles
1.4m replys
5 comments
56.9k users