Re: insert with select as value

From: Harald Fuchs <hf517(at)protecting(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: insert with select as value
Date: 2004-06-23 12:53:12
Message-ID: pud63q8mh3.fsf@srv.protecting.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In article <40D96C94(dot)5040005(at)bvx(dot)cz>,
Milos Prudek <prudek(at)bvx(dot)cz> writes:

>> If your purpose in doing this is just to generate unique keys, you should
>> be using sequences instead.

> I would need 150 separate sequences, because each idsection needs its
> own, independent sequence of idthread.

What you really seem to need is a counter for each idsection.
Depending on how often you need to access the counter value, it might
be worthwile to not store the count at all and instead use a single sequence.
You can compute the counter value at SELECT time by something like

SELECT idsection,
( SELECT count (*)
FROM tbl t
WHERE idsection = tbl.idsection
AND id <= tbl.id
) AS idthread
FROM tbl

where "id" is the single sequence.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Antony Paul 2004-06-23 13:44:26 timestamp without time zone format
Previous Message Richard Huxton 2004-06-23 12:48:15 Re: psql