pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
SQL syntax rowcount value as an extra column in the result set
9+ messages / 4 participants
[nested] [flat]

* SQL syntax rowcount value as an extra column in the result set
@ 2010-03-25 21:33 Snyder, James <jsnyde07@harris.com>
  2010-03-25 21:55 ` Re: SQL syntax rowcount value as an extra column in the result set Thomas Kellerer <spam_eater@gmx.net>
  2010-03-26 03:35 ` Re: SQL syntax rowcount value as an extra column in the result set Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  0 siblings, 2 replies; 9+ messages in thread

From: Snyder, James @ 2010-03-25 21:33 UTC (permalink / raw)
  To: 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

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: SQL syntax rowcount value as an extra column in the result set
  2010-03-25 21:33 SQL syntax rowcount value as an extra column in the result set Snyder, James <jsnyde07@harris.com>
@ 2010-03-25 21:55 ` Thomas Kellerer <spam_eater@gmx.net>
  2010-03-26 03:42   ` Re: SQL syntax rowcount value as an extra column in the result set Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  1 sibling, 1 reply; 9+ messages in thread

From: Thomas Kellerer @ 2010-03-25 21:55 UTC (permalink / raw)
  To: pgsql-sql

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




^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: SQL syntax rowcount value as an extra column in the result set
  2010-03-25 21:33 SQL syntax rowcount value as an extra column in the result set Snyder, James <jsnyde07@harris.com>
  2010-03-25 21:55 ` Re: SQL syntax rowcount value as an extra column in the result set Thomas Kellerer <spam_eater@gmx.net>
@ 2010-03-26 03:42   ` Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  2010-03-26 06:23     ` Re: SQL syntax rowcount value as an extra column in the result set A. Kretschmer <andreas.kretschmer@schollglas.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Jayadevan M @ 2010-03-26 03:42 UTC (permalink / raw)
  To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-sql; pgsql-sql-owner@postgresql.org

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

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: SQL syntax rowcount value as an extra column in the result set
  2010-03-25 21:33 SQL syntax rowcount value as an extra column in the result set Snyder, James <jsnyde07@harris.com>
  2010-03-25 21:55 ` Re: SQL syntax rowcount value as an extra column in the result set Thomas Kellerer <spam_eater@gmx.net>
  2010-03-26 03:42   ` Re: SQL syntax rowcount value as an extra column in the result set Jayadevan M <Jayadevan.Maymala@ibsplc.com>
@ 2010-03-26 06:23     ` A. Kretschmer <andreas.kretschmer@schollglas.com>
  2010-03-26 06:56       ` Re: SQL syntax rowcount value as an extra column in the result set Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  0 siblings, 1 reply; 9+ messages in thread

From: A. Kretschmer @ 2010-03-26 06:23 UTC (permalink / raw)
  To: pgsql-sql

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



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: SQL syntax rowcount value as an extra column in the result set
  2010-03-25 21:33 SQL syntax rowcount value as an extra column in the result set Snyder, James <jsnyde07@harris.com>
  2010-03-25 21:55 ` Re: SQL syntax rowcount value as an extra column in the result set Thomas Kellerer <spam_eater@gmx.net>
  2010-03-26 03:42   ` Re: SQL syntax rowcount value as an extra column in the result set Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  2010-03-26 06:23     ` Re: SQL syntax rowcount value as an extra column in the result set A. Kretschmer <andreas.kretschmer@schollglas.com>
@ 2010-03-26 06:56       ` Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  2010-03-26 07:14         ` Re: SQL syntax rowcount value as an extra column in the result set Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 1 reply; 9+ messages in thread

From: Jayadevan M @ 2010-03-26 06:56 UTC (permalink / raw)
  To: A. Kretschmer <andreas.kretschmer@schollglas.com>; +Cc: pgsql-sql; pgsql-sql-owner@postgresql.org

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

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: SQL syntax rowcount value as an extra column in the result set
  2010-03-25 21:33 SQL syntax rowcount value as an extra column in the result set Snyder, James <jsnyde07@harris.com>
  2010-03-25 21:55 ` Re: SQL syntax rowcount value as an extra column in the result set Thomas Kellerer <spam_eater@gmx.net>
  2010-03-26 03:42   ` Re: SQL syntax rowcount value as an extra column in the result set Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  2010-03-26 06:23     ` Re: SQL syntax rowcount value as an extra column in the result set A. Kretschmer <andreas.kretschmer@schollglas.com>
  2010-03-26 06:56       ` Re: SQL syntax rowcount value as an extra column in the result set Jayadevan M <Jayadevan.Maymala@ibsplc.com>
@ 2010-03-26 07:14         ` Thomas Kellerer <spam_eater@gmx.net>
  2010-03-29 16:25           ` Re: SQL syntax rowcount value as an extra column in the result set Snyder, James <jsnyde07@harris.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Thomas Kellerer @ 2010-03-26 07:14 UTC (permalink / raw)
  To: pgsql-sql

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




^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: SQL syntax rowcount value as an extra column in the result set
  2010-03-25 21:33 SQL syntax rowcount value as an extra column in the result set Snyder, James <jsnyde07@harris.com>
  2010-03-25 21:55 ` Re: SQL syntax rowcount value as an extra column in the result set Thomas Kellerer <spam_eater@gmx.net>
  2010-03-26 03:42   ` Re: SQL syntax rowcount value as an extra column in the result set Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  2010-03-26 06:23     ` Re: SQL syntax rowcount value as an extra column in the result set A. Kretschmer <andreas.kretschmer@schollglas.com>
  2010-03-26 06:56       ` Re: SQL syntax rowcount value as an extra column in the result set Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  2010-03-26 07:14         ` Re: SQL syntax rowcount value as an extra column in the result set Thomas Kellerer <spam_eater@gmx.net>
@ 2010-03-29 16:25           ` Snyder, James <jsnyde07@harris.com>
  2010-03-30 08:03             ` Re: SQL syntax rowcount value as an extra column in the result set Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 1 reply; 9+ messages in thread

From: Snyder, James @ 2010-03-29 16:25 UTC (permalink / raw)
  To: pgsql-sql

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


^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: SQL syntax rowcount value as an extra column in the result set
  2010-03-25 21:33 SQL syntax rowcount value as an extra column in the result set Snyder, James <jsnyde07@harris.com>
  2010-03-25 21:55 ` Re: SQL syntax rowcount value as an extra column in the result set Thomas Kellerer <spam_eater@gmx.net>
  2010-03-26 03:42   ` Re: SQL syntax rowcount value as an extra column in the result set Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  2010-03-26 06:23     ` Re: SQL syntax rowcount value as an extra column in the result set A. Kretschmer <andreas.kretschmer@schollglas.com>
  2010-03-26 06:56       ` Re: SQL syntax rowcount value as an extra column in the result set Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  2010-03-26 07:14         ` Re: SQL syntax rowcount value as an extra column in the result set Thomas Kellerer <spam_eater@gmx.net>
  2010-03-29 16:25           ` Re: SQL syntax rowcount value as an extra column in the result set Snyder, James <jsnyde07@harris.com>
@ 2010-03-30 08:03             ` Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 0 replies; 9+ messages in thread

From: Thomas Kellerer @ 2010-03-30 08:03 UTC (permalink / raw)
  To: pgsql-sql

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




^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: SQL syntax rowcount value as an extra column in the result set
  2010-03-25 21:33 SQL syntax rowcount value as an extra column in the result set Snyder, James <jsnyde07@harris.com>
@ 2010-03-26 03:35 ` Jayadevan M <Jayadevan.Maymala@ibsplc.com>
  1 sibling, 0 replies; 9+ messages in thread

From: Jayadevan M @ 2010-03-26 03:35 UTC (permalink / raw)
  To: Snyder, James <jsnyde07@harris.com>; +Cc: pgsql-sql; pgsql-sql-owner@postgresql.org

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

^ permalink  raw  reply  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2010-03-30 08:03 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-03-25 21:33 SQL syntax rowcount value as an extra column in the result set Snyder, James <jsnyde07@harris.com>
2010-03-25 21:55 ` Thomas Kellerer <spam_eater@gmx.net>
2010-03-26 03:42   ` Jayadevan M <Jayadevan.Maymala@ibsplc.com>
2010-03-26 06:23     ` A. Kretschmer <andreas.kretschmer@schollglas.com>
2010-03-26 06:56       ` Jayadevan M <Jayadevan.Maymala@ibsplc.com>
2010-03-26 07:14         ` Thomas Kellerer <spam_eater@gmx.net>
2010-03-29 16:25           ` Snyder, James <jsnyde07@harris.com>
2010-03-30 08:03             ` Thomas Kellerer <spam_eater@gmx.net>
2010-03-26 03:35 ` Jayadevan M <Jayadevan.Maymala@ibsplc.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox