-- 小时同比
select
    t1.cur_date,
    t2.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), '%')
        when t2.elect = 0 then '0'
        else '上期无数据'
        end mom_elect,
    case
        when t2.cl > 0 then concat(convert(decimal(18, 2),(t1.cl - t2.cl)/ t2.cl * 100), '%')
        when t2.cl = 0 then '0'
        else '上期无数据'
        end mom_cl,
    case
        when t2.cop > 0 then concat(convert(decimal(18, 2),(t1.cop - t2.cop)/ t2.cop * 100), '%')
        when t2.cop = 0 then '0'
        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 >= '2023-11-22 00'
          and cur_date <= '2023-11-22 23'
          and project_id = 3
    ) t2
    on
        t1.change_cur_date = dateadd(year, 1, t2.change_cur_date)
order by t1.cur_date

-- 日同比
select
    t1.cur_date,
    t2.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), '%')
        when t2.elect = 0 then '0'
        else '上期无数据'
        end mom_elect,
    case
        when t2.cl > 0 then concat(convert(decimal(18, 2),(t1.cl - t2.cl)/ t2.cl * 100), '%')
        when t2.cl = 0 then '0'
        else '上期无数据'
        end mom_cl,
    case
        when t2.cop > 0 then concat(convert(decimal(18, 2),(t1.cop - t2.cop)/ t2.cop * 100), '%')
        when t2.cop = 0 then '0'
        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-07-01'
          and cur_date <= '2024-07-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 >= '2023-07-01'
          and cur_date <= '2023-07-22'
          and project_id = 3
    ) t2
    on
        t1.change_cur_date = dateadd(year, 1, t2.change_cur_date)
order by t1.cur_date

-- 月同比
select
    t1.cur_date,
    t2.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), '%')
        when t2.elect = 0 then '0'
        else '上期无数据'
        end mom_elect,
    case
        when t2.cl > 0 then concat(convert(decimal(18, 2),(t1.cl - t2.cl)/ t2.cl * 100), '%')
        when t2.cl = 0 then '0'
        else '上期无数据'
        end mom_cl,
    case
        when t2.cop > 0 then concat(convert(decimal(18, 2),(t1.cop - t2.cop)/ t2.cop * 100), '%')
        when t2.cop = 0 then '0'
        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-11'
          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-01'
          and cur_date <= '2023-07'
          and project_id = 3
    ) t2
    on
        t1.change_cur_date = dateadd(year, 1, t2.change_cur_date)
order by t1.cur_date

-- 年同比
select
    t1.cur_date,
    t2.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), '%')
        when t2.elect = 0 then '0'
        else '上期无数据'
        end mom_elect,
    case
        when t2.cl > 0 then concat(convert(decimal(18, 2),(t1.cl - t2.cl)/ t2.cl * 100), '%')
        when t2.cl = 0 then '0'
        else '上期无数据'
        end mom_cl,
    case
        when t2.cop > 0 then concat(convert(decimal(18, 2),(t1.cop - t2.cop)/ t2.cop * 100), '%')
        when t2.cop = 0 then '0'
        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