From: | Thomas Kellerer <spam_eater(at)gmx(dot)net> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Strict min and max aggregate functions |
Date: | 2016-11-20 11:01:31 |
Message-ID: | o0rvq7$7qm$1@blaine.gmane.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Jeff Janes schrieb am 19.11.2016 um 22:12:
> I need "strict" MIN and MAX aggregate functions, meaning they return
> NULL upon any NULL input, and behave like the built-in aggregates if
> none of the input values are NULL.
>
> This doesn't seem like an outlandish thing to want, and I'm surprised
> I can't find other discussion of it. Perhaps because none of the
> words here are very effective as search terms as they are so
> individually common.
>
> I've hit upon a solution that works, but it is both ugly and slow
> (about 50 fold slower than the built-ins; for my current purpose this
> is not a big problem but I would love it to be faster if that could
> be done easily).
This is not really pretty as well, but might be faster:
select a,
case when group_count = nn_count then min_b end as min_b
from (
select a,
min(b) as min_b,
count(b) as nn_count,
count(*) as group_count
from x
group by a
) t;
As the expensive part is the group by I wouldn't expect the additional aggregates to make a big difference.
Alternatively:
select a, case when no_nulls then min_b end as min_b
from (
select a,
min(b) as min_b,
bool_and(b is not null) as no_nulls
from x
group by a
) t;
From | Date | Subject | |
---|---|---|---|
Next Message | Job | 2016-11-20 12:51:12 | Autovacuum and frequent pg_bulkload |
Previous Message | Pavel Stehule | 2016-11-20 10:45:15 | Re: Strict min and max aggregate functions |