Batching up data into groups of n rows

From: Andy Chambers <achambers(at)mcna(dot)net>
To: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Batching up data into groups of n rows
Date: 2011-09-26 15:59:09
Message-ID: CAAfW55pWbVNijXqjacT4FYjV8w_Srt+MPsp0ApY7W9Xh_LHKyg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I have a need to write a query that batches up rows of a table into
groups of n records. I feel like this should be possible using the
existing window functions but I can't figure out how to do it from the
examples.

So I have some table, let's say

create table addresses (
line_1 text,
line_2 text,
city text,
state text,
zip text);

...and I want to select the data from that table, adding a "group_id"
column, and a "record_id" column. The "group_id" would start at 1,
and increment by 1 every 100 rows, and the "record_id" would increment
by 1 every row, but restart at 1 for each new group_id.

Thanks,
Andy

Responses

Browse pgsql-general by date

  From Date Subject
Next Message hamann.w 2011-09-26 16:18:05 Re: looking for a faster way to do that
Previous Message Alban Hertroys 2011-09-26 15:36:35 Re: Does postgresql 9.0.4 use index on PREFIX%SUFFIX queries?