Use 'COUNT(*) OVER() AS' in postgres while selecting items from a table

6.1k Views Asked by At

I am doing a selection on a table, but also need the count of the items. Apparently I have to use window functions but can't get it work.

I am doing a simple select query and want to count how many entries are there, so something like:

SELECT * FROM "myTable"
COUNT(*) OVER() AS total
WHERE name='John Doe';

This is not working and I am getting the following error: ERROR: ERROR: syntax error at or near "COUNT" LINE 2: COUNT(*) OVER(name) AS _total How do I use the window functions to count my entries in a table while doing a query? Am I way off base here?

1

There are 1 best solutions below

0
On BEST ANSWER

The COUNT(*) is a column in the SELECT:

SELECT t.*, COUNT(*) OVER() AS total
FROM "myTable" t
WHERE name = 'John Doe';

The FROM clause follows the SELECT clause and ends the definitions of the columns in the result set.