-- 小时环比 select t1.cur_date, t1.elect, t1.cl, t1.cop, t1.project_id, t2.elect as last_elect, t2.cl as last_cl, t2.cop as last_cop, case when t2.elect > 0 then concat(convert(decimal(18, 2),(t1.elect - t2.elect)/ t2.elect * 100), '%') else '上期无数据' end mom_elect, case when t2.cl > 0 then concat(convert(decimal(18, 2),(t1.cl - t2.cl)/ t2.cl * 100), '%') else '上期无数据' end mom_cl, case when t2.cop > 0 then concat(convert(decimal(18, 2),(t1.cop - t2.cop)/ t2.cop * 100), '%') else '上期无数据' end mom_cop from ( select cur_date, elect, cl, cop, project_id, convert(datetime, concat(cur_date, ':00:00')) as change_cur_date from energy_data_hour edh1 where cur_date >= '2024-11-22 00' and cur_date <= '2024-11-22 23' and project_id = 3 ) t1 left join ( select cur_date, elect, cl, cop, project_id, convert(datetime, concat(cur_date, ':00:00')) as change_cur_date from energy_data_hour where cur_date >= '2024-11-21 23' and cur_date <= '2024-11-22 22' and project_id = 3 ) t2 on t1.change_cur_date = dateadd(hour, 1, t2.change_cur_date) order by t1.cur_date -- 日环比 select t1.cur_date, t1.elect, t1.cl, t1.cop, t1.project_id, t2.elect as last_elect, t2.cl as last_cl, t2.cop as last_cop, case when t2.elect > 0 then concat(convert(decimal(18, 2),(t1.elect - t2.elect)/ t2.elect * 100), '%') else '上期无数据' end mom_elect, case when t2.cl > 0 then concat(convert(decimal(18, 2),(t1.cl - t2.cl)/ t2.cl * 100), '%') else '上期无数据' end mom_cl, case when t2.cop > 0 then concat(convert(decimal(18, 2),(t1.cop - t2.cop)/ t2.cop * 100), '%') else '上期无数据' end mom_cop from ( select cur_date, elect, cl, cop, project_id, convert(datetime, concat(cur_date, ' 00:00:00')) as change_cur_date from energy_data_day where cur_date >= '2024-11-01' and cur_date <= '2024-11-22' and project_id = 3 ) t1 left join ( select cur_date, elect, cl, cop, project_id, convert(datetime, concat(cur_date, ' 00:00:00')) as change_cur_date from energy_data_day where cur_date >= '2024-10-31' and cur_date <= '2024-11-21' and project_id = 3 ) t2 on t1.change_cur_date = dateadd(day, 1, t2.change_cur_date) order by t1.cur_date -- 月环比 select t1.cur_date, t1.elect, t1.cl, t1.cop, t1.project_id, t2.elect as last_elect, t2.cl as last_cl, t2.cop as last_cop, case when t2.elect > 0 then concat(convert(decimal(18, 2),(t1.elect - t2.elect)/ t2.elect * 100), '%') else '上期无数据' end mom_elect, case when t2.cl > 0 then concat(convert(decimal(18, 2),(t1.cl - t2.cl)/ t2.cl * 100), '%') else '上期无数据' end mom_cl, case when t2.cop > 0 then concat(convert(decimal(18, 2),(t1.cop - t2.cop)/ t2.cop * 100), '%') else '上期无数据' end mom_cop from ( select cur_date, elect, cl, cop, project_id, convert(datetime, concat(cur_date, '-01 00:00:00')) as change_cur_date from energy_data_month where cur_date >= '2024-01' and cur_date <= '2024-12' and project_id = 3 ) t1 left join ( select cur_date, elect, cl, cop, project_id, convert(datetime, concat(cur_date, '-01 00:00:00')) as change_cur_date from energy_data_month where cur_date >= '2023-12' and cur_date <= '2024-11' and project_id = 3 ) t2 on t1.change_cur_date = dateadd(month, 1, t2.change_cur_date) order by t1.cur_date -- 年环比 select t1.cur_date, t1.elect, t1.cl, t1.cop, t1.project_id, t2.elect as last_elect, t2.cl as last_cl, t2.cop as last_cop, case when t2.elect > 0 then concat(convert(decimal(18, 2),(t1.elect - t2.elect)/ t2.elect * 100), '%') else '上期无数据' end mom_elect, case when t2.cl > 0 then concat(convert(decimal(18, 2),(t1.cl - t2.cl)/ t2.cl * 100), '%') else '上期无数据' end mom_cl, case when t2.cop > 0 then concat(convert(decimal(18, 2),(t1.cop - t2.cop)/ t2.cop * 100), '%') else '上期无数据' end mom_cop from ( select cur_date, elect, cl, cop, project_id, convert(datetime, concat(cur_date, '-01-01 00:00:00')) as change_cur_date from energy_data_year where cur_date >= '2023' and cur_date <= '2024' and project_id = 3 ) t1 left join ( select cur_date, elect, cl, cop, project_id, convert(datetime, concat(cur_date, '-01-01 00:00:00')) as change_cur_date from energy_data_year where cur_date >= '2022' and cur_date <= '2023' and project_id = 3 ) t2 on t1.change_cur_date = dateadd(year, 1, t2.change_cur_date) order by t1.cur_date