From: | Bruno Wolff III <bruno(at)wolff(dot)to> |
---|---|
To: | mike <mike(at)bristolreccc(dot)co(dot)uk> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Insert increment based on a group by? |
Date: | 2004-06-04 15:34:37 |
Message-ID: | 20040604153437.GB12744@wolff.to |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Fri, Jun 04, 2004 at 11:39:53 +0100,
mike <mike(at)bristolreccc(dot)co(dot)uk> wrote:
>
> So far I am getting an overall max or no insert.
>
> this is my query
>
> SELECT subj,area, CASE WHEN max(file_no) IS NULL THEN '1' ELSE max
> (file_no)+1 END FROM tb_files GROUP BY (subj||area),subj,area;
You should do a lock table in exclusive mode before doing this, unless
you know nothing else is going on.
An insert would like like:
INSERT INTO table_name (sub, area, file_no) select 'sub_value', 'area_value',
(SELECT max(file_no)+1 FROM table_name where sub = 'sub_value' AND
area = 'area_value')
If you have an index on either area, sub, file_no or sub, area, file_no,
you can rewrite the subselect to use ORDER BY and LIMIT.
It might be better to just assign unique values of file_no for the whole
table. You can use a sequence to support that. That would allow you
to support more concurrency.
From | Date | Subject | |
---|---|---|---|
Next Message | zhicheng wang | 2004-06-04 15:40:11 | Re: after using pg_resetxlog, db lost |
Previous Message | Tom Lane | 2004-06-04 15:20:56 | Re: pg_class could not be found |