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