Re: not so sequential sequences

From: Alexander Staubo <alex(at)purefiction(dot)net>
To: "Rhys Stewart" <rhys(dot)stewart(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: not so sequential sequences
Date: 2006-10-17 22:33:00
Message-ID: 90F55C75-DE79-4E55-A40E-95AB2B7A1C77@purefiction.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Oct 17, 2006, at 23:18 , Rhys Stewart wrote:

> Hi all, looking for a method to number a table sequentially, but the
> sequence only increments if the value in a certain column is
> different. as in
[snip]

Normalization could solve your problem and also improve your schema:

-- The parish table
create table parishes (
id serial primary key,
name varchar
);

-- Your main table
create table ... (
...
parish_id integer references parishes (id)
);

Now you have the parishes table:

id | name
------------------------
1 | Kingston
2 | Lucea
3 | Morant Bay
4 | Port Antonio
5 | Savannah-La-Mar

And your main table:

... | parish_id
------------------------
... | 1
... | 1
... | 1
... | 1
... | 2
... | 3
... | 3
... | 3
... | 4
... | 5
... | 5
... | 5

As you can see, the parish_id field is now your "magic sequence".

> so i guess i would order by a certain column and then the 'magic
> sequence' would be a column that only increments when the column
> changes.

I'm assuming (perhaps wrongly) here that you don't care about a
strictly sequential number; for example, a field indicating the
position of something in a queue or list. I'm also assuming that the
number has no semantic meaning, which might not fit your use case.

Alexander.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ken Tanzer 2006-10-17 22:58:58 Strange behavior on non-existent field in subselect?
Previous Message James Cloos 2006-10-17 22:28:33 Re: Anyone using "POSIX" time zone offset capability?