Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 058289FD222 for ; Wed, 31 Oct 2007 13:46:20 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 02772-02-9 for ; Wed, 31 Oct 2007 13:45:47 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from ns1.officenet.no (vpn.officenet.no [193.212.174.13]) by postgresql.org (Postfix) with ESMTP id 49D4E9FC740 for ; Wed, 31 Oct 2007 12:56:07 -0300 (ADT) Received: from [193.212.174.32] by ns1.officenet.no with esmtpsa (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1InFvA-0004E0-5l for pgsql-sql@postgresql.org; Wed, 31 Oct 2007 16:56:02 +0100 From: Andreas Joseph Krogh Organization: OfficeNet AS To: pgsql-sql Subject: Returning the total number of rows as a separate column when using limit Date: Wed, 31 Oct 2007 16:55:51 +0100 User-Agent: KMail/1.9.6 (enterprise 0.20070907.709405) MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200710311655.51476.andreak@officenet.no> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/154 X-Sequence-Number: 29526 Hi. AFAICS the information about the *total* number of rows is in the "result"= =20 somehow. When I execute a "limit 1" query with EXPLAIN ANALYZE, I se the=20 total number of columns in "rows=3D200819", so the information is there. andreak=3D# EXPLAIN ANALYZE select p.id from onp_crm_person p order by p.cr= eated=20 DESC limit 1; = =20 QUERY PLAN =2D------------------------------------------------------------------------= =2D------------------------------------------------------------------------= =2D---------------- Limit (cost=3D0.00..0.04 rows=3D1 width=3D12) (actual time=3D0.046..0.048= rows=3D1=20 loops=3D1) -> Index Scan Backward using origo_person_created_idx on onp_crm_person= p =20 (cost=3D0.00..8396.45 rows=3D200819 width=3D12) (actual time=3D0.041..0.041= rows=3D1=20 loops=3D1) Total runtime: 0.104 ms (3 rows) Is it possible to use some sort of "magic" function to get this number out = as=20 a separate column? And is this number accurate? It has to be the same as=20 running a separate "count(*)"-query to count the totals, which is exactly=20 what I'm trying to avoid. Oracle has a special rownum and over() which can be used to accomplish this: SELECT tmp.*, max(rownum) over() as total_count FROM (subquery) tmp Does PG have any equivalent way? =2D-=20 Andreas Joseph Krogh Senior Software Developer / Manager =2D-----------------------+---------------------------------------------+ OfficeNet AS | The most difficult thing in the world is to | Karenslyst All=C3=A9 11 | know how to do a thing and to watch | PO. Box 529 Sk=C3=B8yen | somebody else doing it wrong, without | 0214 Oslo | comment. | NORWAY | | Tlf: +47 24 15 38 90 | | =46ax: +47 24 15 38 91 | | Mobile: +47 909 56 963 | | =2D-----------------------+---------------------------------------------+