1 Sequence per Row i.e. each customer's first order starts at 1

From: Merrick <merrick(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: 1 Sequence per Row i.e. each customer's first order starts at 1
Date: 2009-07-02 00:04:33
Message-ID: b1972c6a0907011704i1cf707a1u11c439b09d1958a4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have been using postgresql for 8 years in web projects and ran into
a problem that I could not find a solution for in the archives or
through Google.

Here is a generalized example of what I want to happen. I have a
customers table, and an orders table. I would like for each customer
to have orders that start at 1 and move up sequentially. I realize
it's probably not efficient to create a new sequence for each
customer, so am looking for alternate ways to accomplish the same
thing. Below is an illustrated example of the outcome I would like. I
would also like similar functionality to a sequence so duplicate
order_id's are not generated. Please keep in mind that for what I am
developing, having each customer's orders start at 1 is more of a need
than a want.

Thank you.

Customers
customer_id customer_name
----------------------------------------------------------------
1 acme corp
2 hacker corp
3 dixie corp

Orders
order_id customer_id timestamp
----------------------------------------------------------------
1 1 2009-07-01 14:42
1 2 2009-07-01 14:43
2 2 2009-07-01 14:44
1 3 2009-07-01 14:44
2 1 2009-07-01 14:44
3 1 2009-07-01 14:44

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John Cheng 2009-07-02 00:07:15 Problem search on text arrays, using the overlaps (&&) operator
Previous Message Bruce Momjian 2009-07-01 22:38:42 Re: datestyle no effect in 8.3.7