mysql 按年月日 点击 穿透 获取每年,每月,每日,每小时的数据列表

1.所有年的 数据

            SELECT
		   date_format( t.stat_date, '%Y' ) date_format,           //(t.stat_date数据值 时间字段)
            sum( cast( t.calculation_value AS DECIMAL ( 20, 2 )) ) sum     //(t.calculation_value 数据值)
		  FROM
            energy_date_collect t    //(业务表名)
            WHERE                      // (where 条件)
            t.valid = 1
            AND t.stat_type = '0'
            AND (t.data_type = '01' or t.data_type = '02')
            AND t.energy_use_small = '11'
            GROUP BY date_format

2.穿透年(查询某一年每个月的能耗列表 ) 参数 传 ‘2022’

            SELECT
            i.date,
            IFNULL( j.sum, 0 )
            value
            FROM
            (
            SELECT
            DATE_FORMAT( date_add( concat( '2022', '-01-01' ), INTERVAL ( help_topic_id ) MONTH ), '%Y-%m' ) date
            FROM
            mysql.help_topic
            WHERE
            help_topic_id <= timestampdiff(
            MONTH,
            concat( '2022', '-01-01' ),
            concat( '2022', '-12-01' ))
            ) i
            LEFT JOIN (
            SELECT
            sum( cast( t.calculation_value AS DECIMAL ( 20, 2 )) ) sum,    //(t.calculation_value 数据值)
            date_format( t.stat_date, '%Y-%m' ) date_format     //(t.stat_date数据值 时间字段)
            FROM
            energy_date_collect t      //(业务表名)
            WHERE       // (where 条件)
            t.valid = 1
            AND t.stat_type = '0'
            AND (t.data_type = '01' or t.data_type = '02')
            AND t.energy_use_small = '11'
            GROUP BY
            date_format
            ) j ON i.date = j.date_format

3.穿透月(查询 某一个月每天的能耗列表 ) 参数传 ‘2022-03’

          SELECT
            i.date,
            IFNULL( j.sum, 0 )
            value
            FROM
            (
            SELECT
            DATE_FORMAT( date_add( concat( '2022-03', '-01' ), INTERVAL ( help_topic_id ) DAY ), '%Y-%m-%d' ) date
            FROM
            mysql.help_topic
            WHERE
            help_topic_id <= timestampdiff(
            DAY,
            concat( '2022-03', '-01' ),
            concat(
            date_format( DATE_ADD( str_to_date( concat( '2022-03', '-01' ), '%Y-%m-%d' ), INTERVAL 1 MONTH ), '%Y-%m-%d' ))) - 1
            ) i
            LEFT JOIN (
            SELECT
            sum( cast( t.calculation_value AS DECIMAL ( 20, 2 )) ) sum,    //(t.calculation_value 数据值)
            date_format( t.stat_date, '%Y-%m-%d' ) date_format      //(t.stat_date数据值 时间字段)
            FROM
            energy_date_collect t      //(业务表名)
            WHERE     // (where 条件)
            t.valid = 1
            AND t.stat_type = '0'
            AND (t.data_type = '01' or t.data_type = '02')
            AND t.energy_use_small = '11'
            GROUP BY
            date_format
            ) j ON i.date = j.date_format

4.穿透日(查询一天的每小时的能耗列表) 参数传 ‘2022-03-01’

            SELECT
            a.date,
            IFNULL(sum( cast( b.calculation_value AS DECIMAL ( 20, 2 )) ),0) value    //(t.calculation_value 数据值)
            FROM
            (
            SELECT
            DATE_FORMAT(
            DATE_SUB( DATE_FORMAT( '2022-03-01', '%Y-%m-%d' ), INTERVAL ( - ( @i := @i + 1 ) ) HOUR ),
            '%Y-%m-%d %H'
            ) AS date
            FROM
            (
            SELECT
            a
            FROM
            ( SELECT '1' AS a UNION SELECT '2' UNION SELECT '3' UNION SELECT '4' ) AS a
            JOIN ( SELECT '1' UNION SELECT '2' UNION SELECT '3' UNION SELECT '4' UNION SELECT '5' UNION SELECT '6' ) AS b ON 1
            ) AS b,
            ( SELECT @i :=- 1 ) AS i
            )  a
            LEFT JOIN    //(业务表名)
            energy_date_collect   b  on   a.date  = DATE_FORMAT (b.stat_date,'%Y-%m-%d %H' )     //(t.stat_date数据值 时间字段)
            AND b.valid = 1       // (where 条件)
            AND b.stat_type = '0'
            AND (b.data_type = '01' or b.data_type = '02')
            AND b.energy_use_small = '11'
            GROUP BY a.date
		    ORDER BY date

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OOObject

你的鼓励是我创作的最大源泉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值