How can I define a query, and then run the query and a SELECT COUNT(*) (return the number of records) on that query's result?
I want results similar to:
postgres@127:postgres> (SELECT 1) union all (select 2) union all (select 3);
+----------+
| ?column? |
|----------|
| 1 |
| 2 |
| 3 |
+----------+
SELECT 3
Time: 0.049s
postgres@127:postgres> select count(*) from ((SELECT 1) union all (select 2) union
all (select 3)) as q;
+-------+
| count |
|-------|
| 3 |
+-------+
SELECT 1
It must be a sql statement as I'm running this via pgcli which does not support terminal variables (https://github.com/dbcli/pgcli/issues/829).
I'm trying to use an anonymous PLPGSQL function but struggling to figure it out... And there are multitude of things I can look into next (anonymous code block, using non-anonymous function but then deleting it afterwards, return results from functions (a query instead of a set or rows?)) but I don't know what to focus on. I have this so far:
DO $$DECLARE v record;
BEGIN
SELECT 10,20,30 into v;
END$$;
SELECT COUNT(*) FROM v;
create temp tables v as select ...;then runselect count(*) from v;but I don't understand why you can't runselect count(*) from (select ...) v;directly? – Jan 30 '23 at 08:05I can run it directly, but if I'm often changing the 'main' query that the others depend on (like the
– Chris Stryczynski Jan 30 '23 at 10:53SELECT COUNT(*) ...it gets a bit cumbersome (text manipulation + number of button presses) having to copy/paste and rerun each query.