Commit 98fc1979 authored by 吴文龙's avatar 吴文龙

增加pg有关能源报表脚本

parent 454b896e
...@@ -2863,8 +2863,404 @@ INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "p ...@@ -2863,8 +2863,404 @@ INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "p
INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('8b33ceb0-3fa7-11ed-bef7-83ea816b6ac8', 'XTCD1664422434587', '班组能耗', 1, 28, 'https://lowcode-engine.textile-saas.huansi.net/view.html?scenarioName=index&pageKey=junlin_group_detail', '5faac9b0-3fa7-11ed-bef7-83ea816b6ac8', '', NULL, 'Global', 1666158658234, 'abd5b000-38ce-11ed-be80-751173af0b60', '1666158658234', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', '/iframe/groupEnergy', 'f', '班组能耗'); INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('8b33ceb0-3fa7-11ed-bef7-83ea816b6ac8', 'XTCD1664422434587', '班组能耗', 1, 28, 'https://lowcode-engine.textile-saas.huansi.net/view.html?scenarioName=index&pageKey=junlin_group_detail', '5faac9b0-3fa7-11ed-bef7-83ea816b6ac8', '', NULL, 'Global', 1666158658234, 'abd5b000-38ce-11ed-be80-751173af0b60', '1666158658234', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', '/iframe/groupEnergy', 'f', '班组能耗');
INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('66ab57e0-4842-11ed-af8a-cbe90563c5e2', 'XTCD1665368554590', '会计月度能耗', 1, 29, 'https://lowcode-engine.textile-saas.huansi.net/view.html?scenarioName=index&pageKey=junlin_month_detail', '5faac9b0-3fa7-11ed-bef7-83ea816b6ac8', NULL, NULL, 'Global', 1666158681840, 'abd5b000-38ce-11ed-be80-751173af0b60', '1666158681840', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', '/iframe/repMonth', 'f', '会计月度能耗'); INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('66ab57e0-4842-11ed-af8a-cbe90563c5e2', 'XTCD1665368554590', '会计月度能耗', 1, 29, 'https://lowcode-engine.textile-saas.huansi.net/view.html?scenarioName=index&pageKey=junlin_month_detail', '5faac9b0-3fa7-11ed-bef7-83ea816b6ac8', NULL, NULL, 'Global', 1666158681840, 'abd5b000-38ce-11ed-be80-751173af0b60', '1666158681840', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', '/iframe/repMonth', 'f', '会计月度能耗');
INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('e5562890-37e9-11ed-aa50-33c3c4d399b5', 'XTCD1663571323289', '设备配置', 1, 13, NULL, '8b394f90-37e9-11ed-aa50-33c3c4d399b5', 'mdi:alpha-d-box', NULL, 'Global', 1683277031369, '85088750-1ea1-11ed-a8be-71239561244e', '1683277031369', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', '/profiles/deviceProfiles', 'f', 'device-profile.device-profiles'); INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('e5562890-37e9-11ed-aa50-33c3c4d399b5', 'XTCD1663571323289', '设备配置', 1, 13, NULL, '8b394f90-37e9-11ed-aa50-33c3c4d399b5', 'mdi:alpha-d-box', NULL, 'Global', 1683277031369, '85088750-1ea1-11ed-a8be-71239561244e', '1683277031369', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', '/profiles/deviceProfiles', 'f', 'device-profile.device-profiles');
INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('d7bf1190-4569-11ee-91cb-23f59766f287', 'XTCD1693203138857', '能源报表', 0, 52, NULL, NULL, 'mdi:table', NULL, 'Global', 1693203624072, 'abd5b000-38ce-11ed-be80-751173af0b60', '1693203624072', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', NULL, 'f', '能源报表');
INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('a2360a70-4568-11ee-91cb-23f59766f287', 'XTCD1693202619543', '工厂日能耗分析', 1, 51, 'http://192.168.8.105:8888/#/fDailyEnergyanalysis', 'd7bf1190-4569-11ee-91cb-23f59766f287', NULL, NULL, 'Global', 1693205134680, 'abd5b000-38ce-11ed-be80-751173af0b60', '1693205134680', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', '/iframe/dailyEnergyanalysis', 'f', '工厂日能耗分析');
INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('20f490a0-456b-11ee-91cb-23f59766f287', 'XTCD1693203691178', '半点能耗统计', 1, 53, 'http://192.168.8.105:8888/#/pubHalfEnergy', 'd7bf1190-4569-11ee-91cb-23f59766f287', NULL, NULL, 'Global', 1693228951835, 'abd5b000-38ce-11ed-be80-751173af0b60', '1693228951835', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', '/iframe/pubHalfEnergy', 'f', '半点能耗统计');
INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('5b05ad60-456b-11ee-91cb-23f59766f287', 'XTCD1693203788598', '仪表当前读数', 1, 54, 'http://192.168.8.105:8888/#/pub_latest_telemetry', 'd7bf1190-4569-11ee-91cb-23f59766f287', NULL, NULL, 'Global', 1693229035599, 'abd5b000-38ce-11ed-be80-751173af0b60', '1693229035599', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', '/iframe/pub_latest_telemetry', 'f', '仪表当前读数');
INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('941f4ac0-456b-11ee-91cb-23f59766f287', 'XTCD1693203884396', '能源仪表用量', 1, 55, 'http://192.168.8.105:8888/#/f_pub_trep_energy_detail', 'd7bf1190-4569-11ee-91cb-23f59766f287', NULL, NULL, 'Global', 1693229041552, 'abd5b000-38ce-11ed-be80-751173af0b60', '1693229041552', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', '/iframe/f_pub_trep_energy_detail', 'f', '能源仪表用量');
INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('ca8e6190-456b-11ee-91cb-23f59766f287', 'XTCD1693203975721', '工厂能源分析', 1, 56, 'http://192.168.8.105:8888/#/factory_energy', 'd7bf1190-4569-11ee-91cb-23f59766f287', NULL, NULL, 'Global', 1693229048015, 'abd5b000-38ce-11ed-be80-751173af0b60', '1693229048015', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', '/iframe/factoryEnergyAnalysis', 'f', '工厂能源分析');
INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('dde84cb0-456b-11ee-91cb-23f59766f287', 'XTCD1693204008187', '车间能源分析', 1, 57, 'http://192.168.8.105:8888/#/work_energy', 'd7bf1190-4569-11ee-91cb-23f59766f287', NULL, NULL, 'Global', 1693229053450, 'abd5b000-38ce-11ed-be80-751173af0b60', '1693229053450', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', '/iframe/workEnergyAnalysis', 'f', '车间能源分析');
INSERT INTO "public"."tb_menu" ("id", "code", "name", "level", "sort", "url", "parent_id", "menu_icon", "menu_images", "region", "created_time", "created_user", "updated_time", "updated_user", "menu_type", "path", "is_button", "lang_key") VALUES ('f5c1a570-456b-11ee-91cb-23f59766f287', 'XTCD1693204048199', '设备能源分析', 1, 58, 'http://192.168.8.105:8888/#/device_energy', 'd7bf1190-4569-11ee-91cb-23f59766f287', NULL, NULL, 'Global', 1693229058813, 'abd5b000-38ce-11ed-be80-751173af0b60', '1693229058813', 'abd5b000-38ce-11ed-be80-751173af0b60', 'PC', '/iframe/EquipmentEnergyAnalysis', 'f', '设备能源分析');
update tb_user set email='sysadmin@huansi.net' ,search_text ='sysadmin@huansi.net' where authority='SYS_ADMIN'; update tb_user set email='sysadmin@huansi.net' ,search_text ='sysadmin@huansi.net' where authority='SYS_ADMIN';
update user_credentials set "password" ='$2a$10$NwAGVsFrb3p74FY9oKKogurUfKlO9DrS3DXrSK40OmRLvjj/xegZy' where user_Id =(select id from tb_user where authority='SYS_ADMIN'); update user_credentials set "password" ='$2a$10$NwAGVsFrb3p74FY9oKKogurUfKlO9DrS3DXrSK40OmRLvjj/xegZy' where user_Id =(select id from tb_user where authority='SYS_ADMIN');
-- PG 报表函数
CREATE OR REPLACE FUNCTION "public"."tmp_fname_energy"("start_year" int4, "end_year" int4, "tid" varchar)
RETURNS "pg_catalog"."varchar" AS $BODY$
/*
table_name : 表名
row_name : 行名字段
cat : 转换为列名的字段
value_field : 转换为值的字段
*/
DECLARE
v_sql text;
table_name_tmp text;
BEGIN
table_name_tmp = 'tmp_fname_energy_table';
v_sql = 'SELECT
ROW_NUMBER() OVER (ORDER BY fname) AS "序号",
fname as "工厂名称",
name as "能源类型",
units as "单位",
SUM(val_lud::NUMERIC-val_std::NUMERIC) as "累计用量"';
FOR _year IN start_year..end_year LOOP
v_sql = v_sql||', SUM(CASE WHEN EXTRACT(YEAR FROM ctime) = '''||_year||''' THEN val_lud::NUMERIC - val_std::NUMERIC ELSE 0 END) AS "'||_year||'"';
END LOOP;
v_sql = v_sql||' FROM
trep_energy
WHERE
fname is not null and tid='''||tid||''' and
ctime BETWEEN '''||start_year||'-01-01'||''' and '''||end_year||'-12-31'||'''
GROUP BY EXTRACT(YEAR FROM ctime), fname, name, units ORDER BY fname, name';
v_sql='
drop table if exists '||table_name_tmp||';
CREATE TABLE '||table_name_tmp||' as '||v_sql;
EXECUTE v_sql;
return v_sql;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION "public"."tmp_fname_energy"("start_year" int4, "end_year" int4, "tid" varchar) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."tmp_device_energy"("start_year" int4, "end_year" int4, "tid" varchar)
RETURNS "pg_catalog"."varchar" AS $BODY$
/*
table_name : 表名
row_name : 行名字段
cat : 转换为列名的字段
value_field : 转换为值的字段
*/
DECLARE
v_sql text;
table_name_tmp text;
BEGIN
table_name_tmp = 'tmp_device_energy_table';
v_sql = 'SELECT
ROW_NUMBER() OVER (ORDER BY fname) AS "序号",
fname as "工厂名称",
wname as "车间名称",
dname as "设备名称",
name as "能源类型",
units as "单位",
SUM(val_lud::NUMERIC-val_std::NUMERIC) as "累计用量"';
FOR _year IN start_year..end_year LOOP
v_sql = v_sql||', SUM(CASE WHEN EXTRACT(YEAR FROM ctime) = '''||_year||''' THEN val_lud::NUMERIC - val_std::NUMERIC ELSE 0 END) AS "'||_year||'"';
END LOOP;
v_sql = v_sql||' FROM
trep_energy
WHERE
fname is not null and tid='''||tid||''' and
ctime BETWEEN '''||start_year||'-01-01'||''' and '''||end_year||'-12-31'||'''
GROUP BY EXTRACT(YEAR FROM ctime), fname, wname, dname, name, units ORDER BY fname, wname, dname, name';
v_sql='
drop table if exists '||table_name_tmp||';
CREATE TABLE '||table_name_tmp||' as '||v_sql;
EXECUTE v_sql;
return v_sql;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION "public"."tmp_device_energy"("start_year" int4, "end_year" int4, "tid" varchar) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."tmp_wname_energy"("start_year" int4, "end_year" int4, "tid" varchar)
RETURNS "pg_catalog"."varchar" AS $BODY$
/*
table_name : 表名
row_name : 行名字段
cat : 转换为列名的字段
value_field : 转换为值的字段
*/
DECLARE
v_sql text;
table_name_tmp text;
BEGIN
table_name_tmp = 'tmp_wname_energy_table';
v_sql = 'SELECT
ROW_NUMBER() OVER (ORDER BY fname) AS "序号",
fname as "工厂名称",
wname as "车间名称",
name as "能源类型",
units as "单位",
SUM(val_lud::NUMERIC-val_std::NUMERIC) as "累计用量"';
FOR _year IN start_year..end_year LOOP
v_sql = v_sql||', SUM(CASE WHEN EXTRACT(YEAR FROM ctime) = '''||_year||''' THEN val_lud::NUMERIC - val_std::NUMERIC ELSE 0 END) AS "'||_year||'"';
END LOOP;
v_sql = v_sql||' FROM
trep_energy
WHERE
fname is not null and tid='''||tid||''' and
ctime BETWEEN '''||start_year||'-01-01'||''' and '''||end_year||'-12-31'||'''
GROUP BY EXTRACT(YEAR FROM ctime), fname, wname, name, units ORDER BY fname, wname, name';
v_sql='
drop table if exists '||table_name_tmp||';
CREATE TABLE '||table_name_tmp||' as '||v_sql;
EXECUTE v_sql;
return v_sql;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION "public"."tmp_wname_energy"("start_year" int4, "end_year" int4, "tid" varchar) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."generate_dynamic_table_workshop"("year_value" int4, "month_value" int4, "tid" text)
RETURNS "pg_catalog"."void" AS $BODY$
DECLARE
dynamic_sql TEXT;
date_values TEXT;
BEGIN
-- 构建日期列的排序字符串
SELECT string_agg('SUM(CASE WHEN date = ''' || date_value::TEXT || ''' THEN total END) AS "' || date_value::TEXT || '"', ',')
INTO date_values
FROM (
SELECT DISTINCT date_value::TEXT
FROM generate_month_dates(year_value, month_value)
ORDER BY date_value
) subquery;
-- 构建动态SQL查询
dynamic_sql := '
SELECT name,''工厂名称'' as fname,wname,units, ' || date_values || ', SUM(total) AS total_energy
FROM (
SELECT name,''工厂名称'' as fname,wname,units, DATE_TRUNC(''day'', ctime) AS date, round(SUM(TO_NUMBER(val_lud,''9999999999999999999.999'') - TO_NUMBER(val_std,''9999999999999999999.999'')),2) AS total
FROM trep_energy t
WHERE DATE_TRUNC(''day'', t.ctime) IN (
SELECT date_value
FROM generate_month_dates($1, $2)
)
AND t.tid = $3::UUID -- Filter by tid parameter
GROUP BY name, date, fname, wname, units
) subquery
GROUP BY name, fname, wname, units
ORDER BY fname,wname, name';
-- 删除临时表
EXECUTE 'DROP TABLE IF EXISTS tmp_target_table_workshop';
-- 创建临时表并插入数据
EXECUTE 'CREATE TABLE tmp_target_table_workshop AS ' || dynamic_sql
USING year_value, month_value, tid;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION "public"."generate_dynamic_table_workshop"("year_value" int4, "month_value" int4, "tid" text) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."generate_dynamic_table_f"("year_value" int4, "month_value" int4, "tid" text)
RETURNS "pg_catalog"."void" AS $BODY$
DECLARE
dynamic_sql TEXT;
date_values TEXT;
BEGIN
-- 构建日期列的排序字符串
SELECT string_agg('SUM(CASE WHEN date = ''' || date_value::TEXT || ''' THEN total END) AS "' || date_value::TEXT || '"', ',') INTO date_values
FROM (
SELECT DISTINCT date_value::TEXT
FROM generate_month_dates(year_value, month_value)
ORDER BY date_value
) subquery;
-- 构建动态SQL查询
dynamic_sql := '
SELECT ROW_NUMBER() OVER (ORDER BY fname, name) AS serial_number, name, fname, units, ' || date_values || ', SUM(total) as total_energy
FROM (
SELECT name, fname, units, DATE_TRUNC(''day'', ctime) AS date, round(SUM(TO_NUMBER(val_lud,''9999999999999999999.999'') - TO_NUMBER(val_std,''9999999999999999999.999'')),2) AS total
FROM trep_energy t
WHERE DATE_TRUNC(''day'', t.ctime) IN (
SELECT date_value
FROM generate_month_dates($1, $2)
)
AND t.tid = $3::UUID -- Filter by tid
GROUP BY name, date, fname, units
) subquery
GROUP BY name, fname, units
ORDER BY fname, name';
-- 删除临时表
EXECUTE 'DROP TABLE IF EXISTS tmp_target_table_f';
-- 创建临时表并插入数据
EXECUTE 'CREATE TABLE tmp_target_table_f AS ' || dynamic_sql
USING year_value, month_value, tid;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION "public"."generate_dynamic_table_f"("year_value" int4, "month_value" int4, "tid" text) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."generate_dynamic_table_jt"("year_value" int4, "month_value" int4, "tid" text, "wid" text=''::text)
RETURNS "pg_catalog"."void" AS $BODY$
DECLARE
dynamic_sql TEXT;
date_values TEXT;
BEGIN
-- 构建日期列的排序字符串
SELECT string_agg('SUM(CASE WHEN date = ''' || date_value::TEXT || ''' THEN total END) AS "' || date_value::TEXT || '"', ',') INTO date_values
FROM (
SELECT DISTINCT date_value::TEXT
FROM generate_month_dates(year_value, month_value)
ORDER BY date_value
) subquery;
-- 构建动态SQL查询
dynamic_sql := '
SELECT ROW_NUMBER() OVER (ORDER BY fname) AS serial_number, name, fname, wname, dname, units, ' || date_values || ', SUM(total) as total_energy
FROM (
SELECT name, fname, wname, dname, units, DATE_TRUNC(''day'', ctime) AS date, round(SUM(TO_NUMBER(val_lud,''9999999999999999999.999'') - TO_NUMBER(val_std,''9999999999999999999.999'')),2) AS total
FROM trep_energy t
WHERE DATE_TRUNC(''day'', t.ctime) IN (
SELECT date_value
FROM generate_month_dates($1, $2)
)
AND t.tid = $3::UUID
AND (t.wid = $4::UUID OR $4 IS NULL)
GROUP BY name, date, fname, wname, dname, units
) subquery
GROUP BY name, fname, wname, dname, units
ORDER BY fname,wname, dname, name';
-- 删除临时表
EXECUTE 'DROP TABLE IF EXISTS tmp_target_table_jt';
-- 创建临时表并插入数据
EXECUTE 'CREATE TEMPORARY TABLE tmp_target_table_jt AS ' || dynamic_sql
USING year_value, month_value, tid, NULLIF(wid, '');
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION "public"."generate_dynamic_table_jt"("year_value" int4, "month_value" int4, "tid" text, "wid" text=''::text) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."tmp_denergy_month"("year_int" int4, "tid" varchar)
RETURNS "pg_catalog"."varchar" AS $BODY$
/*
table_name : 表名
row_name : 行名字段
cat : 转换为列名的字段
value_field : 转换为值的字段
*/
DECLARE v_sql text;
table_name_tmp text;
arow record;
BEGIN
table_name_tmp = 'tmp_denergy_month';
v_sql = 'SELECT
ROW_NUMBER() OVER (ORDER BY wname) AS "wname",
wname as "车间名称",
name as "能源类型",
dname as "设备名称",
units as "单位",
SUM(val_lud::NUMERIC-val_std::NUMERIC) as "累计用量"';
FOR arow IN (SELECT month_value FROM generate_year_months(2023)) LOOP
v_sql = v_sql||', SUM(CASE WHEN EXTRACT(month FROM ctime) = '''||arow.month_value||''' THEN val_lud::NUMERIC - val_std::NUMERIC ELSE 0 END) AS "'||arow.month_value||'"';
END LOOP;
v_sql = v_sql||' FROM
trep_energy
WHERE
fname is not null and tid='''||tid||''' and
EXTRACT(YEAR FROM ctime)='||year_int||'
GROUP BY EXTRACT(month FROM ctime), wname, dname, name, units ORDER BY wname, dname, name';
v_sql='
drop table if exists '||table_name_tmp||';
CREATE TABLE '||table_name_tmp||' as '||v_sql;
EXECUTE v_sql;
return v_sql;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION "public"."tmp_denergy_month"("year_int" int4, "tid" varchar) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."tmp_fenergy_month"("year_int" int4, "tid" varchar)
RETURNS "pg_catalog"."varchar" AS $BODY$
/*
table_name : 表名
row_name : 行名字段
cat : 转换为列名的字段
value_field : 转换为值的字段
*/
DECLARE v_sql text;
table_name_tmp text;
arow record;
BEGIN
table_name_tmp = 'tmp_fenergy_month';
v_sql = 'SELECT
ROW_NUMBER() OVER (ORDER BY fname) AS "序号",
fname as "工厂名称",
name as "能源类型",
units as "单位",
SUM(val_lud::NUMERIC-val_std::NUMERIC) as "累计用量"';
FOR arow IN (SELECT month_value FROM generate_year_months(year_int)) LOOP
v_sql = v_sql||', SUM(CASE WHEN EXTRACT(month FROM ctime) = '''||arow.month_value||''' THEN val_lud::NUMERIC - val_std::NUMERIC ELSE 0 END) AS "'||arow.month_value||'"';
END LOOP;
v_sql = v_sql||' FROM
trep_energy
WHERE
fname is not null and tid='''||tid||''' and
EXTRACT(YEAR FROM ctime)='||year_int||'
GROUP BY EXTRACT(month FROM ctime), fname, name, units ORDER BY fname, name';
v_sql='
drop table if exists '||table_name_tmp||';
CREATE TABLE '||table_name_tmp||' as '||v_sql;
EXECUTE v_sql;
return v_sql;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION "public"."tmp_fenergy_month"("year_int" int4, "tid" varchar) OWNER TO "postgres";
CREATE OR REPLACE FUNCTION "public"."tmp_wenergy_month"("year_int" int4, "tid" varchar)
RETURNS "pg_catalog"."varchar" AS $BODY$
/*
table_name : 表名
row_name : 行名字段
cat : 转换为列名的字段
value_field : 转换为值的字段
*/
DECLARE v_sql text;
table_name_tmp text;
arow record;
BEGIN
table_name_tmp = 'tmp_wenergy_month';
v_sql = 'SELECT
ROW_NUMBER() OVER (ORDER BY wname) AS "序号",
wname as "车间名称",
name as "能源类型",
units as "单位",
SUM(val_lud::NUMERIC-val_std::NUMERIC) as "累计用量"';
FOR arow IN (SELECT month_value FROM generate_year_months(2023)) LOOP
v_sql = v_sql||', SUM(CASE WHEN EXTRACT(month FROM ctime) = '''||arow.month_value||''' THEN val_lud::NUMERIC - val_std::NUMERIC ELSE 0 END) AS "'||arow.month_value||'"';
END LOOP;
v_sql = v_sql||' FROM
trep_energy
WHERE
fname is not null and tid='''||tid||''' and
EXTRACT(YEAR FROM ctime)='||year_int||'
GROUP BY EXTRACT(month FROM ctime), wname, name, units ORDER BY wname, name';
v_sql='
drop table if exists '||table_name_tmp||';
CREATE TABLE '||table_name_tmp||' as '||v_sql;
EXECUTE v_sql;
return v_sql;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION "public"."tmp_wenergy_month"("year_int" int4, "tid" varchar) OWNER TO "postgres";
\ No newline at end of file
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