Write an aggregate to count the number of times the number 40 is seen in a column.
Use your aggregate to count the number of 40 year olds in the directory table.
This is what I was doing:
Create function aggstep(curr int) returns int as $$
begin
return curr.count where age = 40;
end;
$$ language plpgsql;
Create aggregate aggs(integer) (
stype = int,
initcond = '',
sfunc = aggstep);
Select cas(age) from directory;
You could do it for example like this:
First, create a transition function:
That works because
FALSE::integer
is 0 andTRUE::integer
is 1.I use
IS NOT DISTINCT FROM
rather than=
so that it does the correct thing for NULLs.The aggregate can then be defined as
You can then query like