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

From: "Snyder, James" <jsnyde07(at)harris(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: SQL syntax rowcount value as an extra column in the result set
Date: 2010-03-25 21:33:59
Message-ID: 594D625E4784AD4EA9D423E292BE6725489FD1@mlbe2k10.cs.myharris.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Thomas Kellerer 2010-03-25 21:55:41 Re: SQL syntax rowcount value as an extra column in the result set
Previous Message Tom Lane 2010-03-25 16:55:54 Re: Does IMMUTABLE property propagate?