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

Поиск
Список
Период
Сортировка
Искать

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

От:
"Snyder, James" <jsnyde07@harris.com>
Дата:

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

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

От:
Thomas Kellerer <spam_eater@gmx.net>
Дата:
Snyder, James wrote on 25.03.2010 22:33:

> I’m using PostgreSQL (8.4.701)
There is no such version.
The current version is 8.4.3


> On a side note, Oracle allows the following syntax to achieve the above:
>
> select count(*) over () as ROWCOUNT , first_name from people
>
The same syntax will work on Postgres

Thomas


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

От:
"Jayadevan M" <Jayadevan.Maymala@ibsplc.com>
Дата:
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@harris.com>
To:        <pgsql-sql@postgresql.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@postgresql.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 ROWCOUNTfrom 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."

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

От:
"Jayadevan M" <Jayadevan.Maymala@ibsplc.com>
Дата:
Hi,
I don't think so.
Oracle -
SQL> select count(*) over () as ROWCOUNT , first_name from people;

  ROWCOUNT FIRST_NAME
---------- ---------------------------------------------------------------------
-------------------------------
         6 Mary
         6 Mary
         6 John
         6 John
         6 John
         6 Jacob

6 rows selected.

PostgreSQL
postgres=# select count(*) over () as ROWCOUNT , first_name from people;
ERROR:  syntax error at or near "over"
LINE 1: select count(*) over () as ROWCOUNT , first_name from people...
                        ^
Regards,
Jayadevan



From:        Thomas Kellerer <spam_eater@gmx.net>
To:        pgsql-sql@postgresql.org
Date:        26/03/2010 03:26
Subject:        Re: [SQL] SQL syntax rowcount value as an extra column in the result set
Sent by:        pgsql-sql-owner@postgresql.org




Snyder, James wrote on 25.03.2010 22:33:

> I’m using PostgreSQL (8.4.701)
There is no such version.
The current version is 8.4.3


> On a side note, Oracle allows the following syntax to achieve the above:
>
> select count(*) over () as ROWCOUNT , first_name from people
>
The same syntax will work on Postgres

Thomas


--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

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."

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

От:
"A. Kretschmer" <andreas.kretschmer@schollglas.com>
Дата:
In response to Jayadevan M :
> Hi,
> I don't think so.
> Oracle -
> SQL> select count(*) over () as ROWCOUNT , first_name from people;
> 
>   ROWCOUNT FIRST_NAME
> ----------
> ---------------------------------------------------------------------
> -------------------------------
>          6 Mary
>          6 Mary
>          6 John
>          6 John
>          6 John
>          6 Jacob
> 
> 6 rows selected.
> 
> PostgreSQL
> postgres=# select count(*) over () as ROWCOUNT , first_name from people;
> ERROR:  syntax error at or near "over"
> LINE 1: select count(*) over () as ROWCOUNT , first_name from people...

It works, but you should use a recent version:

test=*# select count(1) over (), i from foo;count | i
-------+----    8 |  1    8 |  2    8 |  3    8 |  6    8 |  7    8 |  9    8 | 13    8 | 14
(8 rows)

test=*# select version();                                               version
--------------------------------------------------------------------------------------------------------PostgreSQL 8.4.2 on x86_64-pc-linux-gnu, compiled by GCC gcc-4.3.real
(Debian 4.3.2-1.1) 4.3.2, 64-bit
(1 row)

test=*#

Regards, Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431  2EB0 389D 1DC2 3172 0C99

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

От:
"Jayadevan M" <Jayadevan.Maymala@ibsplc.com>
Дата:
Hi,
>It works, but you should use a recent version:

>test=*# select count(1) over (), i from foo;
> count | i
>-------+----
>     8 |  1
>     8 |  2
>     8 |  3
>     8 |  6
>     8 |  7
>     8 |  9
>     8 | 13
>     8 | 14
>(8 rows)

> test=*# select version();
>                                                version
> --------------------------------------------------------------------------------------------------------
>  PostgreSQL 8.4.2 on x86_64-pc-linux-gnu, compiled by GCC gcc-4.3.real
> (Debian 4.3.2-1.1) 4.3.2, 64-bit
> (1 row)

Thank you for setting that right. Apologies for not checking version.
Is this approach better compared to
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)
This gives me
postgres=# explain  select * from (select count(*) from people )as p, (select firstname from people)p2;
                             QUERY PLAN
---------------------------------------------------------------------
 Nested Loop  (cost=14.00..30.42 rows=320 width=226)
   ->  Aggregate  (cost=14.00..14.01 rows=1 width=0)
         ->  Seq Scan on people  (cost=0.00..13.20 rows=320 width=0)
   ->  Seq Scan on people  (cost=0.00..13.20 rows=320 width=218)

Since I don't have 8.4, I am not in a position to do explain on that version. My guess - over () will be better. My query does sequential scans/nested loop...(if there are no indexes)

Regards,
Regards,
Jayadevan 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."

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

От:
Thomas Kellerer <spam_eater@gmx.net>
Дата:
Jayadevan M, 26.03.2010 07:56:
> Thank you for setting that right. Apologies for not checking version.

The orginal poster stated that he is using 8.4, so that solution will work for him.

Thomas


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

От:
"Snyder, James" <jsnyde07@harris.com>
Дата:
Thanks for all the dialog on this subject.

My "version" was derived from the postgreSQL's .jar file (specifically named "postgresql-8.4-701.jdbc4.jar") that I'm using. When I do the following:

select version()

I get the following:

PostgreSQL 8.3.6

I'm going to check this out.

Thanks...Jim


-----Original Message-----
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Thomas Kellerer
Sent: Friday, March 26, 2010 3:15 AM
To: pgsql-sql@postgresql.org
Subject: Re: [SQL] SQL syntax rowcount value as an extra column in the result set


Jayadevan M, 26.03.2010 07:56:
> Thank you for setting that right. Apologies for not checking version.

The orginal poster stated that he is using 8.4, so that solution will work for him.

Thomas


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

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

От:
Thomas Kellerer <spam_eater@gmx.net>
Дата:
Snyder, James, 29.03.2010 18:25:
> Thanks for all the dialog on this subject.
>
> My "version" was derived from the postgreSQL's .jar file (specifically named "postgresql-8.4-701.jdbc4.jar") that I'm using. When I do the following:
>
> select version()
>
> I get the following:
>
> PostgreSQL 8.3.6
>

Then you cannot use the new windowing functions, you will need to upgrade to 8.4

Thomas


FAQ