Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1j6t1f-0006nH-2v for pgsql-general@arkaria.postgresql.org; Wed, 26 Feb 2020 09:30:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j6t1d-0006Hb-UL for pgsql-general@arkaria.postgresql.org; Wed, 26 Feb 2020 09:30:41 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j6t1d-0006HU-H0 for pgsql-general@lists.postgresql.org; Wed, 26 Feb 2020 09:30:41 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j6t1T-00044T-Ci for pgsql-general@lists.postgresql.org; Wed, 26 Feb 2020 09:30:40 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 761AF2590; Wed, 26 Feb 2020 10:30:28 +0100 (CET) Date: Wed, 26 Feb 2020 10:30:28 +0100 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Invoice increment Message-ID: <20200226093028.GA10408@hjp.at> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="jI8keyz6grp/JLjh" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --jI8keyz6grp/JLjh Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2020-02-26 09:38:57 +0100, S=F8ren Frisk wrote: > I'm trying to find a way to increment an invoice number. And i think it w= ould > be pretty straight forward just using a SERIAL int. Be careful: Sequences may have gaps (e.g. because of rolled back transactions, but maybe also for other reasons). In many jurisdictions, invoice numbers must not have gaps. You may still be able to use sequences, but you have to carefully consider when to increment them. > But as we're selling across > multiple countries, i need the invoice increment to be reset by a country= _id. By "reset by a country_id" you mean that the invoice number has to increment independently for each country? So if you sell for example, to=20 DK, DK, CN, DK, CN, US, DK, US, the invoice numbers should be DK-1 DK-2 CN-1 DK-3 CN-2 US-1 DK-4 US-2 ? You can do that by having one sequence per country. The parameter to nextval is just a string, so you can construct that on the fly: select nextval('invoice_seq_' || country_code); However, that means that you have to add a new sequence whenever you start business in a new country. Maybe not a big deal, but easy to forget since you don't do that every day. The other way is of course to have a table with all the current invoice numbers for each country. Basically sequences implemented in the application. This has a slightly different set of problems, so you have to be careful, too. hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --jI8keyz6grp/JLjh Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAl5WOq8ACgkQ8g5IURL+ KF1DORAAja9nkxnVsYF8Prrm6aqBkhhxsvNZKNaA41+3qeohryCW91I4BJvqfGpR Dft6GDPvBfGD4PHiZErMQLIERjS8b4RcW5DT9HJ5L/Yrh5nSeLBqhLpfc9K/f5yF sbgysBbz+HMrFseqkJEC+2sfhYwt3xD/aAnFVEos3/LWhJyMYI2VUY7QA0aV2EUL 6JFs1QyL2qxb1f5vu8Q3wd2KBQEx4QMt/mopUpUEWTN04KZwkEjgfqMAmNZFa4uU ULO0+Qfxv3v3v/kiihZaMmIB2m6A3hsCp7BgSKqnQZ+klFsyRLZAXihufErblIqC ZcrXB4oOjE8sZgdL9cXdays/0sQ9jBAwNBclvqc+ZcSM3ZfbEK1pTG/06yE91/+d jSb/JHM+vv3uC/NMDWCS23p08Rf8Dyy5o+yLx4pukDWkmHNBbhmvhtvoHFVensU/ sutIVNOVwYtyq/xXOCKps5hSM3GtZKazFfhpGp2uZhYxewaqF0SlhpyKIBVPaOZY mx+r8J84AcWxFF+r3KKkZ23kFIbaI9O7XTaMrgVDwi+I7Fuv0lGr8na3jHoojHkd ONN5kaGE4BM0Hn0tls0AVKnGb6xV6d8g0A6WWiM7bObFBYWg0tIY5qSH/IocP6kA pH3+CgkhOAJ7bMmFIeJiu1Gd6wH/kUGgCJYFbHQSiKgv0F+UNT0= =IjnR -----END PGP SIGNATURE----- --jI8keyz6grp/JLjh--