Discussion:
Row Count
(too old to reply)
Sergey Gigoyan
2013-02-12 07:05:20 UTC
Permalink
How can I get selected row count in one statement?
For example, I can do that by two select statements

select id, name, addrress, (select count(*) from(select id
from
users
where
name<>'examplename')
from users
where name<>'examplename'

or

declare rowcount integer
...............
rowcount:=select count(*) from(select id
from users
where name<>'examplename')
as rst

select id, name, address, rowcount as rowcount
from users
where name<>'examplename'
Thomas Kellerer
2013-02-12 08:00:33 UTC
Permalink
Post by Sergey Gigoyan
How can I get selected row count in one statement?
For example, I can do that by two select statements
select id, name, addrress, (select count(*) from(select id
from users
where name<>'examplename')
from users
where name<>'examplename'
select id,
name,
addrress,
count(*) over () as row_count
from users
where name <> 'examplename';
--
Sent via pgsql-novice mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice
Andreas Kretschmer
2013-02-12 08:03:02 UTC
Permalink
Post by Sergey Gigoyan
How can I get selected row count in one statement?
For example, I can do that by two select statements
select id, name, addrress, (select count(*) from(select id
select id, name, ..., row_number() over () from ...

works since 8.4.


Andreas
--
Sent via pgsql-novice mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice
Thomas Kellerer
2013-02-12 08:18:12 UTC
Permalink
Post by Andreas Kretschmer
select id, name, ..., row_number() over () from ...
works since 8.4.
That will only return the row count in the *last* row, not in all of them.
--
Sent via pgsql-novice mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice
Sergey Konoplev
2013-02-12 08:07:21 UTC
Permalink
On Mon, Feb 11, 2013 at 11:05 PM, Sergey Gigoyan
Post by Sergey Gigoyan
How can I get selected row count in one statement?
Usually it is immediately available after statement execution without
additional query.

For example in PL/PgSQL you can use GET DIAGNOSTICS straight after your SELECT:

SELECT INTO _id, _name, _address id, name, address
FROM users WHERE name <> 'examplename';

GET DIAGNOSTICS _row_count = ROW_COUNT;

In external languages like Perl:

$sth = $dbh->prepare(
"select id, name, address, rowcount as rowcount".
"from users where name<>'examplename'");

$row_count = $sth->execute(...);
Post by Sergey Gigoyan
For example, I can do that by two select statements
select id, name, addrress, (select count(*) from(select id
from users
where
name<>'examplename')
from users
where name<>'examplename'
or
declare rowcount integer
...............
rowcount:=select count(*) from(select id
from users
where name<>'examplename')
as rst
select id, name, address, rowcount as rowcount
from users
where name<>'examplename'
--
Sergey Konoplev
Database and Software Architect
http://www.linkedin.com/in/grayhemp

Phones:
USA +1 415 867 9984
Russia, Moscow +7 901 903 0499
Russia, Krasnodar +7 988 888 1979

Skype: gray-hemp
Jabber: ***@gmail.com
--
Sent via pgsql-novice mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice
Loading...