gpt4 book ai didi

ruby-on-rails - 重构 - 找到一个函数来将列添加到 postgres 查询

转载 作者:行者123 更新时间:2023-11-29 12:26:15 25 4
gpt4 key购买 nike

我希望将以下查询添加为 Rails 应用程序中的数据库 View :

select
recruiters.full_name as recruiter,
advisors.advisor as agent,
sum(case when policies.week_number=1 then policies.premium else 0 end) as wk1,
sum(case when policies.week_number=2 then policies.premium else 0 end) as wk2,
sum(case when policies.week_number=3 then policies.premium else 0 end) as wk3,
sum(case when policies.week_number=4 then policies.premium else 0 end) as wk4,
sum(policies.premium) as total
from policies
join advisors on policies.advisor_id=advisors.id
join recruiters on advisors.recruiter_id=recruiters.id
where policies.current_status='T' or policies.current_status='I'
group by recruiters.full_name, advisors.advisor

在 select 语句中,我目前每周添加一个 sum 语句来创建新列(在代码中创建重复)。这将导致一整年的结果如下:

sum(case when policies.week_number=1 then policies.premium else 0 end) as wk1,
sum(case when policies.week_number=2 then policies.premium else 0 end) as wk2,
....
....
sum(case when policies.week_number=52 then policies.premium else 0 end) as wk52,
sum(case when policies.week_number=53 then policies.premium else 0 end) as wk53

结果如下:

Week 1: 
RECRUITER | AGENT | WK1
----------|------------|----------
MC | DM | 523.8
MC | BO | 0.0

Week 2:
RECRUITER | AGENT | WK1 | WK2
----------|------------|----------|---------
MC | DM | 523.8 | -540.0
MC | BO | 0.0 | 0.0

....

Week 4:
RECRUITER | AGENT | WK1 | WK2 | WK3 | WK4 | TOTAL
----------|------------|----------|---------|----------|----------|---------
MC | DM | 523.8 | -540.0 | 358.44 | 510.0 | 852.24
MC | BO | 0.0 | 0.0 | 1543.72 | 0.0 | 1543.72

我想用类似于以下逻辑的东西重构代码:

  1. 在数据集中找到可用周数的uniq集合:

    => [1,2,3,4]
  2. 对于每个招聘人员/代理配对的每周总和 policies.premium。在 rails 中,我会尝试像这样将 select 语句附加到查询中:

    table_columns = []
    [1,2,3,4].each do |i|
    table_columns << sum(case when policies.week_number=#{i} then policies.premium else 0 end) as wk#{i}
    end

例如,当 policies.week_number=5 的数据可用时,将为一年中的所有 53 周添加“wk5”列等。

是否可以在 Postgres 中创建一个函数来适应这样的重构?

仅供引用..我尝试使用交叉表,但我也在计算分组招聘人员的小计。

最佳答案

好吧,基于你的那个例子,我确实写了一个 plpgsql 函数,它根据输入变量折射和动态构建一个 sql 查询 - 在这种情况下:请求周数。

该查询还有很大的改进空间,因此它更易于阅读,但见鬼...这只是一个快速 cooking 。

DROP FUNCTION if exists stack_test(integer[]);

CREATE OR REPLACE FUNCTION stack_test(integer[])
RETURNS text AS
$BODY$
declare
rec record;
sql text;

select_field_sum text;
core_crostab_1 text;
core_crostab_2 text;
ct_field_list text ;

begin

select_field_sum ='0.0';
core_crostab_1 ='';
core_crostab_2 ='';
ct_field_list ='';

FOR rec IN select unnest(( $1)) i order by 1
loop
ct_field_list = ct_field_list ||',wk' || (rec.i)::text || ' numeric' ;
select_field_sum = select_field_sum || '+coalesce(wk' || (rec.i)::text|| ',0)' ;
end loop;

core_crostab_1 = 'select advisor_id, week_number,sum(premium) premium from policies where week_number = any ( array['|| array_to_string($1, ',') ||'] ) and policies.current_status=''''T'''' or policies.current_status=''''I'''' group by advisor_id,week_number order by 1,2';
core_crostab_2 = 'select unnest((array['|| array_to_string($1, ',') ||'] )) order by 1';

sql = 'select *, ('||select_field_sum||') premiumsum from crosstab('''||core_crostab_1||''', '''||core_crostab_2||''') as ct(advisor_id integer '|| ct_field_list ||') ';
sql = 'select recruiters.full_name as requiter,advisors.advisor ,q1.* from ( '||sql||')q1 left join advisors on(q1.advisor_id= advisors.id ) left join recruiters on (advisors.recruiter_id = recruiters.id) order by 1,2';

return sql::text;
end;
$BODY$
LANGUAGE plpgsql volatile
COST 100;

关于ruby-on-rails - 重构 - 找到一个函数来将列添加到 postgres 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35418668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com