Re: SQL syntax rowcount value as an extra column in the result set

From: "Jayadevan M" <Jayadevan(dot)Maymala(at)ibsplc(dot)com>
To: "Snyder, James" <jsnyde07(at)harris(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org,pgsql-sql-owner(at)postgresql(dot)org
Subject: Re: SQL syntax rowcount value as an extra column in the result set
Date: 2010-03-26 03:35:00
Message-ID: OF5A2D0504.FE37F1EE-ON652576F2.00139075-652576F2.00138CC0@LocalDomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi,
Is this what you are trying to do?
postgres=# select * from (select count(*) from people ) p, (select
firstname from people)p2;
count | firstname
-------+-----------
5 | Mary
5 | Mary
5 | John
5 | John
5 | Jacob
(5 rows)
I do not know about the performance impact of such a query (cartesian
join)
Regards,
Jayadevan

From: "Snyder, James" <jsnyde07(at)harris(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Date: 26/03/2010 03:21
Subject: [SQL] SQL syntax rowcount value as an extra column in the
result set
Sent by: pgsql-sql-owner(at)postgresql(dot)org

Hello
I’m using PostgreSQL (8.4.701) and Java (jdbc,
postgresql-8.4-701.jdbc4.jar) to connect to the database.
My question is: what is the SQL syntax for PostgreSQL to achieve the
following:
I want to receive the rowcount along with the rest of a result set. For
example, let’s say the following query returns
select first_name from people;
first_name
=========
Mary
Sue
Joe

and the following query returns the value
select count(*)as ROWCOUNT from people;
ROWCOUNT
==========
3
3
What I’m looking for is the output as
ROWCOUNT , first_name
=====================
3 , Mary
3 , Sue
3 , Joe
so I can use JDBC (snip-it) as follows:
resultSet.getInt(“ROWCOUNT”)
resultSet.getString(“first_name”)
On a side note, Oracle allows the following syntax to achieve the above:
select count(*) over () as ROWCOUNT , first_name from people
Thanks,Jim

DISCLAIMER: "The information in this e-mail and any attachment is intended only for the person to whom it is addressed and may contain confidential and/or privileged material. If you have received this e-mail in error, kindly contact the sender and destroy all copies of the original communication. IBS makes no warranty, express or implied, nor guarantees the accuracy, adequacy or completeness of the information contained in this email or any attachment and is not liable for any errors, defects, omissions, viruses or for resultant loss or damage, if any, direct or indirect."

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jayadevan M 2010-03-26 03:42:01 Re: SQL syntax rowcount value as an extra column in the result set
Previous Message Thomas Kellerer 2010-03-25 21:55:41 Re: SQL syntax rowcount value as an extra column in the result set