Re: Can I get the number of results plus the results with a single query?

From: Wim Bertels <wim(dot)bertels(at)ucll(dot)be>
To: "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Can I get the number of results plus the results with a single query?
Date: 2022-08-22 07:17:09
Message-ID: c26e951ed0b6074536deef1a94f2a0b30c35f506.camel@ucll.be
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Perry Smith schreef op ma 15-08-2022 om 08:49 [-0500]:
> I like to have what I call “baby sitting” messages such as “Completed
> 15 out of 1023”.  To do this, I need the number of results a query
> returns but I also need the results.
>
> Currently I’m doing this with two queries such as:
>
>     SELECT COUNT(*) FROM table WHERE …. expression …
>     SELECT * FROM table WHERE …. expression …
>
> But this requires two queries.  Is there a way to do the same thing
> with just one query somehow?
>
> I’ve been toying with row_number() and then sort by row_number
> descending and pick off the first row as the total number.  

if i understand your question:

count(*) over ()

(u can use aggregate functions as well, not only window functions)

mvg,
Wim

In response to

Browse pgsql-general by date

  From Date Subject
Next Message sivapostgres@yahoo.com 2022-08-22 07:29:01 Re: Creating constraint dynamically
Previous Message Tom Lane 2022-08-21 18:04:12 Re: Support for dates before 4713 BC