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

From: "Jayadevan M" <Jayadevan(dot)Maymala(at)ibsplc(dot)com>
To: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(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 06:56:54
Message-ID: OF3566EE01.958C8348-ON652576F2.002579A1-652576F2.002608F3@LocalDomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Thomas Kellerer 2010-03-26 07:14:50 Re: SQL syntax rowcount value as an extra column in the result set
Previous Message A. Kretschmer 2010-03-26 06:45:29 Re: Help me with this multi-table query