database - PostgreSQL data missing from view -
because postgresql doesn't support dynamically calculated columns of table, 1 possible way overcome limitation use views pointed in answer this question. tried implement such view in following way:
create view user_info_view select *, calculate_bonus_points_for_period(user_info.id) bonus_points_period, calculate_user_level(user_info.id) level user_info;
where user_info
table following:
create table user_info ( id serial primary key, card_id text not null unique, name text not null, address text not null, phone text not null, bank_credit bigint not null, deposit bigint not null, bonus_points bigint not null );
and 2 functions used calculate view columns following:
create type user_level enum ('bronze', 'gold', 'platinum'); ---------------------------------------------------------------------- create function calculate_bonus_points_for_period(user_id integer) returns bigint $body$ declare bonus_period smallint; result bigint; begin select settings.bonus_period bonus_period settings; select sum(bonus_points_earned) result game_pledges game_pledges.user_id = calculate_bonus_points_for_period.user_id , ts > current_date - interval '1 day' * bonus_period; return coalesce(result, 0); end; $body$ language plpgsql; ---------------------------------------------------------------------- create function calculate_user_level(user_id integer) returns user_level $body$ declare bonus_points_period bigint; bronze_gold_boundery bigint; gold_platinum_boundery bigint; begin select user_info_view.bonus_points_period bonus_points_period user_info_view user_id = id; select settings.bronze_gold_boundery bronze_gold_boundery settings; select settings.gold_platinum_boundery gold_platinum_boundery settings; if bonus_points_period >= gold_platinum_boundery return 'platinum'; elsif bonus_points_period >= bronze_gold_boundery return 'gold'; else return 'bronze'; end if; end; $body$ language plpgsql;
i have data in user_info
table view empty when opened pgadmin
. in beginning have no data in game_pledges
table , expected sum
0
. can explain why view empty? i'm expecting there same number of rows in user_info
table.
afterwords:
when executed select count(*) user_info_view;
found have few ambiguities in functions. corrected code above resolved ambiguities, have error:
error: stack depth limit exceeded hint: increase configuration parameter "max_stack_depth" (currently 2048kb), after ensuring platform's stack depth limit adequate. context: sql statement "select sum(bonus_points_earned) game_pledges game_pledges.user_id = calculate_bonus_points_for_period.user_id , ts > current_date - interval '1 day' * bonus_period"
Comments
Post a Comment