ENH break histogram function into more convenient chunks
This commit is contained in:
parent
59e5edc066
commit
c138528915
|
@ -4,36 +4,62 @@ as $$
|
||||||
select ts::time at time zone 'US/Eastern';
|
select ts::time at time zone 'US/Eastern';
|
||||||
$$ language SQL;
|
$$ language SQL;
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION histogram(tname text, cname text, width int)
|
create or replace function get_bin_num(x numeric, width int)
|
||||||
RETURNS TABLE(bin_number int, bin_interval text, count bigint)
|
returns int
|
||||||
|
as $$
|
||||||
|
-- select (floor(x/width)*width)::int;
|
||||||
|
select floor(x/width)::int;
|
||||||
|
$$ language SQL;
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION bin_ranges(tname text, cname text, width int)
|
||||||
|
RETURNS TABLE(bin_number int, bin_interval text)
|
||||||
AS $func$
|
AS $func$
|
||||||
BEGIN
|
BEGIN
|
||||||
RETURN QUERY EXECUTE format('
|
RETURN QUERY EXECUTE format('
|
||||||
with recursive
|
with recursive
|
||||||
no_bins_t as (
|
no_bins_t as (
|
||||||
select min(floor(a.%1$s/%2$s)*%2$s)::int as bin_number from %3$s a
|
select min(get_bin_num(%1$s,%2$s)) as bin_number from %3$s a
|
||||||
|
|
||||||
union all
|
union all
|
||||||
|
|
||||||
select bin_number + 1 as bin_number from no_bins_t
|
select bin_number + 1 as bin_number from no_bins_t
|
||||||
where
|
where
|
||||||
bin_number < (select max(floor(a.%1$s/%2$s)) + 1 from %3$s a)
|
bin_number < (select max(get_bin_num(%1$s,%2$s)) + 1 from %3$s a)
|
||||||
)
|
)
|
||||||
|
|
||||||
select
|
select
|
||||||
bin_number,
|
bin_number,
|
||||||
concat(bin_number*%2$s, ''-'', bin_number*%2$s + %2$s - 1) as bin_interval,
|
concat(bin_number*%2$s, ''-'', bin_number*%2$s + %2$s - 1) as bin_interval
|
||||||
case when count is null then 0 else count end as count
|
|
||||||
from no_bins_t
|
from no_bins_t
|
||||||
left join
|
order by no_bins_t.bin_number;',
|
||||||
(select
|
cname,
|
||||||
floor(a.%1$s/%2$s)*%2$s as ab_floor,
|
width,
|
||||||
|
tname
|
||||||
|
);
|
||||||
|
END
|
||||||
|
$func$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION histogram(tname text, cname text, width int)
|
||||||
|
RETURNS TABLE(bin_number int, bin_interval text, count bigint)
|
||||||
|
AS $func$
|
||||||
|
BEGIN
|
||||||
|
RETURN QUERY EXECUTE format('
|
||||||
|
with binned as (
|
||||||
|
select
|
||||||
|
get_bin_num(%1$s,%2$s) as ab_floor,
|
||||||
count(*) as count
|
count(*) as count
|
||||||
from %3$s a
|
from %3$s a
|
||||||
group by ab_floor
|
group by ab_floor
|
||||||
order by ab_floor) as binned
|
order by ab_floor
|
||||||
on no_bins_t.bin_number*%2$s=binned.ab_floor
|
)
|
||||||
order by no_bins_t.bin_number;',
|
|
||||||
|
select
|
||||||
|
bin_number,
|
||||||
|
bin_interval,
|
||||||
|
case when b.count is null then 0 else b.count end as count
|
||||||
|
from bin_ranges(''%3$s'',''%1$s'',%2$s) r
|
||||||
|
left join binned b on r.bin_number=b.ab_floor
|
||||||
|
order by r.bin_number;',
|
||||||
cname,
|
cname,
|
||||||
width,
|
width,
|
||||||
tname
|
tname
|
||||||
|
|
Loading…
Reference in New Issue