Commit fa4e549b by lijingjing

更改统计查询,去除非必要函数;

parent a57b9078
......@@ -291,15 +291,16 @@
i.company_id,
u.period,
i.entrust_sample_id,
round(sum(case when ii.name = '磷生铁-C' then to_number(ii.test_value,'9999.99') else 0.00 end) / func_if_int(sum(case when ii.name = '磷生铁-C' then 1 else 0 end) = 0,1,sum(case when ii.name = '磷生铁-C' then 1 else 0 end)),2) "c_val",
round(sum(case when ii.name = '磷生铁-Si' then to_number(ii.test_value,'9999.99') else 0.00 end) / func_if_int(sum(case when ii.name = '磷生铁-Si' then 1 else 0 end) = 0,1,sum(case when ii.name = '磷生铁-C' then 1 else 0 end)),2) "si_val",
round(sum(case when ii.name = '磷生铁-Mn' then to_number(ii.test_value,'9999.99') else 0.00 end) / func_if_int(sum(case when ii.name = '磷生铁-Mn' then 1 else 0 end) = 0,1,sum(case when ii.name = '磷生铁-Mn' then 1 else 0 end)),2) "mn_val",
round(sum(case when ii.name = '磷生铁-P' then to_number(ii.test_value,'9999.99') else 0.00 end) / func_if_int(sum(case when ii.name = '磷生铁-P' then 1 else 0 end) = 0,1,sum(case when ii.name = '磷生铁-P' then 1 else 0 end)),2) "p_val",
round(sum(case when ii.name = '磷生铁-S' then to_number(ii.test_value,'9999.99') else 0.00 end) / func_if_int(sum(case when ii.name = '磷生铁-S' then 1 else 0 end) = 0,1,sum(case when ii.name = '磷生铁-S' then 1 else 0 end)),2) "s_val"
round(sum(case when ii.name = '磷生铁-C' then to_number(ii.test_value,'9999.99') else 0.00 end) / coalesce(sum(case when ii.name = '磷生铁-C' then 1 else null end),1),2) "c_val",
round(sum(case when ii.name = '磷生铁-Si' then to_number(ii.test_value,'9999.99') else 0.00 end) / coalesce(sum(case when ii.name = '磷生铁-Si' then 1 else null end),1),2) "si_val",
round(sum(case when ii.name = '磷生铁-Mn' then to_number(ii.test_value,'9999.99') else 0.00 end) / coalesce(sum(case when ii.name = '磷生铁-Mn' then 1 else null end),1),2) "mn_val",
round(sum(case when ii.name = '磷生铁-P' then to_number(ii.test_value,'9999.99') else 0.00 end) / coalesce(sum(case when ii.name = '磷生铁-P' then 1 else null end),1),2) "p_val",
round(sum(case when ii.name = '磷生铁-S' then to_number(ii.test_value,'9999.99') else 0.00 end) / coalesce(sum(case when ii.name = '磷生铁-S' then 1 else null end),1),2) "s_val"
FROM
entrust_sample_item i,
entrust_sample_item_index ii,
dblink ('pc_base', 'select * from v_user_period') as u ( user_id int8, period VARCHAR)
-- 此视图需要关联base数据库查询,具体查看设计视图
v_sys_user_period U
WHERE
i.ID = ii.entrust_sample_item_id
AND I.TESTER_ID = u.USER_ID
......@@ -314,6 +315,7 @@
) PI
)
select
pi.period,
round(sum(pi.c_val) / count(1),2) "c_val",
round(sum(pi.si_val) / count(1),2) "si_val",
round(sum(pi.mn_val) / count(1),2) "mn_val",
......
-- 开启dblink拓展
++ /dev/null
-- 开启dblink拓展
--执行:create extension dblink;
create extension dblink;
-- 创建数据库链接
SELECT dblink_connect('dev_base', 'host=localhost dbname=dev_base user=postgres password=post123');
SELECT dblink_connect('dev_lims', 'host=localhost dbname=dev_lims user=postgres password=post123');
SELECT dblink_connect('pc_base', 'host=localhost dbname=pc_base user=hjhmdb password=hm57hj6');
SELECT dblink_connect('pc_lims', 'host=localhost dbname=pc_lims user=hjhmdb password=hm57hj6');
-------------------------------------------------------------------------------------------------
-- 创建字符,数字处理函数
CREATE OR REPLACE FUNCTION "public"."func_decode"(VARIADIC "p_decode_list" _text)
RETURNS "pg_catalog"."text" AS $BODY$
declare
-- 获取数组长度(即入参个数)
v_len integer := array_length(p_decode_list, 1);
-- 声明存放返回值的变量
v_ret text;
begin
/*
* 功能说明:模拟Oracle中的DECODE功能(字符串处理, 其它格式可以自行转换返回值)
* 参数说明:格式同Oracle相同,至少三个参数
* 实现原理: 1、VARIADIC 允许变参; 2、Oracle中的DECODE是拿第一个数依次和之后的偶数位值进行比较,相同则取偶数位+1的数值,否则取最后一位值(最后一位为偶数为,否则为null)
*/
-- 同Oracle相同当参数不足三个抛出异常
if v_len >= 3 then
-- Oracle中的DECODE是拿第一个数依次和之后的偶数位值进行比较,相同则取偶数位+1的数值
for i in 2..(v_len - 1) loop
v_ret := null;
if mod(i, 2) = 0 then
if p_decode_list[1] = p_decode_list[i] then
v_ret := p_decode_list[i+1];
elsif p_decode_list[1] <> p_decode_list[i] then
if v_len = i + 2 and v_len > 3 then
v_ret := p_decode_list[v_len];
end if;
end if;
end if;
exit when v_ret is not null;
end loop;
else
raise exception 'UPG-00938: not enough args for function.';
end if;
return v_ret;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
------------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION "public"."func_if"("p_condition" bool, "p_fist_val" text, "p_last_val" text)
RETURNS "pg_catalog"."text" AS $BODY$
declare
v_ret_val text;
begin
/*
* 功能说明:模拟三元表达式 ( condition ? value1 : value2 );
* 参数说明:p_condition 接收 boolean类型的表达式 如: 1 = 1, 2 > 1 等; 后两个值是根据p_condition的真假对应的返回值
* 实现原理: p_condition 为真则返回p_fist_val, 否则返回P_last_val
*/
v_ret_val := null;
if true = p_condition then
v_ret_val := p_fist_val;
elsif false = p_condition then
v_ret_val := P_last_val;
end if;
return v_ret_val;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
--------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION "public"."func_if_int"("p_condition" bool, "p_fist_val" int8, "p_last_val" int8)
RETURNS "pg_catalog"."int8" AS $BODY$
declare
v_ret_val int8;
begin
/*
* 功能说明:模拟三元表达式 ( condition ? value1 : value2 );
* 参数说明:p_condition 接收 boolean类型的表达式 如: 1 = 1, 2 > 1 等; 后两个值是根据p_condition的真假对应的返回值
* 实现原理: p_condition 为真则返回p_fist_val, 否则返回P_last_val
*/
v_ret_val := null;
if true = p_condition then
v_ret_val := p_fist_val;
elsif false = p_condition then
v_ret_val := P_last_val;
end if;
return v_ret_val;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
----------------------------------------------------------------------------------
-- 开启dblink拓展
-- 开启dblink拓展
--执行:create extension dblink;
create extension dblink;
-------------------------------------------------------------------------------------------------
-- 创建获取数据库信息函数
CREATE OR REPLACE FUNCTION "public"."func_db_info"("db_name" varchar='pc_base'::character varying)
RETURNS "pg_catalog"."varchar" AS $BODY$BEGIN
-- Routine body goes here...
IF(strpos(db_name,'dev_') > 0) then
RETURN 'host=localhost dbname='|| db_name || ' user=postgres password=post123';
ELSE
RETURN 'host=localhost dbname='|| db_name || ' user=hjhmdb password=hm57hj6';
END IF;
END$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
--- 新增视图 :系统用户所属期数 ---
create or replace view v_sys_user_period
as
SELECT u.user_id,
u.period
FROM dblink(func_db_info('dev_base'::character varying)::text, ' SELECT rel.user_id, CASE WHEN sum(strpos(r.name::text, ''三期''::text)) > 0 THEN ''三期'' ELSE ''二期'' END AS period FROM sys_user_rel_role rel JOIN sys_role r ON r.id = rel.role_id WHERE r.status = 1 GROUP BY rel.user_id'::text) u(user_id bigint, period character varying)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment