Re: insert with select as value - Mailing list pgsql-general

From Harald Fuchs
Subject Re: insert with select as value
Date
Msg-id pud63q8mh3.fsf@srv.protecting.net
Whole thread Raw
In response to insert with select as value  (Milos Prudek <prudek@bvx.cz>)
List pgsql-general
In article <40D96C94.5040005@bvx.cz>,
Milos Prudek <prudek@bvx.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.

pgsql-general by date:

Previous
From: Richard Huxton
Date:
Subject: Re: psql
Next
From: "Antony Paul"
Date:
Subject: timestamp without time zone format