SELECTstring_agg('SUM(CASE WHEN date = '''||date_value::TEXT||''' THEN total END) AS "'||date_value::TEXT||'"',',')
INTOdate_values
FROM(
SELECTDISTINCTdate_value::TEXT
FROMgenerate_month_dates(year_value,month_value)
ORDERBYdate_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
SELECTstring_agg('SUM(CASE WHEN date = '''||date_value::TEXT||''' THEN total END) AS "'||date_value::TEXT||'"',',')INTOdate_values
FROM(
SELECTDISTINCTdate_value::TEXT
FROMgenerate_month_dates(year_value,month_value)
ORDERBYdate_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
SELECTstring_agg('SUM(CASE WHEN date = '''||date_value::TEXT||''' THEN total END) AS "'||date_value::TEXT||'"',',')INTOdate_values
FROM(
SELECTDISTINCTdate_value::TEXT
FROMgenerate_month_dates(year_value,month_value)
ORDERBYdate_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
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||'"';
ENDLOOP;
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=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||'"';
ENDLOOP;
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=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||'"';
ENDLOOP;
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';