触发器
作者:互联网
基本流程:
触发器触发时,取出api_import_batch的id,去api_betting_detail去统计数据插入到api_import_total表中,
需要判断根据account1,platform,betting_date去判断数据是否存在,存在则更新,不存在则插入。
type_code字段需要联合tps_platform_info,tps_game_type表查出type_code。
select account1,platform,betting_date,sum(profit_loss),sum(real_betting_amount),sum(betting_num)
from api_betting_detail where import_batch_id=1 group by account1,platform,betting_date;
update api_import_total set profit_loss_amount=1,betting_amount=1,betting_amount=1,type_code='type_code',batch_id=1 where account1=1 and betting_date='' and platform=1;
数据汇总完成之后,需要对api_import_total再进行汇总一次。然后将汇总后的数据更新到ge_dashboard_total 表中。
select account1,betting_date,sum(profit_loss_amount),sum(betting_amount) from api_import_total where batch_id=1 group by account1,betting_date;
update ge_dashboard_tatol set profit_loss_amount=1,betting_amount=1 where account1=account1 and collect_date=betting_date;
--创建触发器事件
CREATE TRIGGER "GPO".update_api_import_batch_trg
BEFORE UPDATE
ON "GPO".mny_com_record
FOR EACH ROW
EXECUTE PROCEDURE "GPO".update_api_import_batch();
--创建触发器事
--select account1,platform,betting_date,sum(profit_loss),sum(real_betting_amount),sum(betting_num) from api_betting_detail where import_batch_id=old.id group by account1,platform,betting_date;
select c.type_code from api_betting_detail a
left join tps_platform_info b
on ( a.platform = b.platform_code )
left join tps_game_type c
on ( b.type_id = c.id )
where a.import_batch_id=new.id
CREATE FUNCTION "GPO".update_api_import_batch() RETURNS trigger LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
begin
if (TG_OP = 'UPDATE' and old.complete_date <> new.complete_date )
then
if ( select account1,platform,betting_date from api_betting_detail where import_batch_id=new.id and not exists( select * from api_import_total) ) then
insert into
select a.account1,a.platform,a.betting_date,c.type_code,25185,sum(a.profit_loss),sum(a.real_betting_amount),sum(a.betting_num) from api_betting_detail a
left join tps_platform_info b on ( a.platform = b.platform_code )
left join tps_game_type c on ( b.type_id = c.id )
where a.import_batch_id=new.id group by a.account1,a.platform,a.betting_date,c.type_code;
--利用 insert into on conflict do update语句进行判断(有的话进行update,没有的话,进行insert)
else
update
end if;
end if;
RETURN new;
end;
$BODY$;
ALTER FUNCTION "GPO".update_api_import_batch() OWNER TO "GPO";
select *
from A
where not exists(select 1 from B where A.a = B.b)
select a.account1,a.platform,a.betting_date,c.type_code,new.id,sum(a.profit_loss),sum(a.real_betting_amount),sum(a.betting_num) from api_betting_detail a
left join tps_platform_info b
on ( a.platform = b.platform_code )
left join tps_game_type c
on ( b.type_id = c.id )
where a.import_batch_id=new.id
group by a.account1,a.platform,a.betting_date,c.type_code;
标签:触发器,betting,platform,api,date,import,id 来源: https://www.cnblogs.com/hmwh/p/11360506.html