月報、區間、年報 增加總計資料列 v1.0

This commit is contained in:
cesar liu 2022-07-30 02:06:19 +08:00
parent 848d57ca30
commit 81777eab68

View File

@ -51,19 +51,36 @@ namespace SolarPower.Repository.Implement
conn.Open(); conn.Open();
try try
{ {
string sql = "SET SESSION group_concat_max_len = 102400;"; //string sql = @$"SET SESSION group_concat_max_len = 102400;
sql += @$" // SELECT GROUP_CONCAT( 'a.inv_',RIGHT(v.InverterId, 2))
SET @sql = NULL; // from inv_status v
SELECT GROUP_CONCAT( 'a.inv_',RIGHT(v.InverterId, 2)) // LEFT JOIN power_station p ON p.Code = left(inverterid, 9)
from v_company_inv v // WHERE p.Id = {post.PowerStation} AND v.enabled = 1
// order by 1";
string sql = @$" SELECT RIGHT(v.InverterId, 2) inv
from inv_status v
LEFT JOIN power_station p ON p.Code = left(inverterid, 9) LEFT JOIN power_station p ON p.Code = left(inverterid, 9)
WHERE p.Id = {post.PowerStation} AND v.enabled = 1 ;"; WHERE p.Id = {post.PowerStation} AND v.enabled = 1
var inv = conn.Query<string>(sql).FirstOrDefault(); order by 1";
var invList = conn.Query<string>(sql).ToList();
string inv = string.Empty;
foreach (var s in invList)
{
inv += "round(sum( inv_" + s + ") , 2) inv_" + s + " , ";
}
// sum(inv_01) as inv_01 // sum(inv_01) as inv_01
sql = $@" select GROUP_CONCAT( ' round(sum(inv_',RIGHT(v.InverterId, 2), '), 3) inv_',RIGHT(v.InverterId, 2) ) //var inv_ss = inv.Split(',').Select(x => " round(sum(" + x + "), 2) " + x.Substring(2, x.Length -2) + ", ").ToList();
from inv_status v LEFT JOIN power_station p ON p.Code = left(inverterid, 9) //string inv_sum = string.Empty;
WHERE p.Id = {post.PowerStation} and v.enabled = 1;"; //foreach (string s in inv_ss)
var inv_sum = conn.Query<string>(sql).FirstOrDefault(); //{
// inv_sum += s;
//}
//sql = $@" select GROUP_CONCAT( ' round(sum(inv_',RIGHT(v.InverterId, 2), '), 3) inv_',RIGHT(v.InverterId, 2) )
// from inv_status v LEFT JOIN power_station p ON p.Code = left(inverterid, 9)
// WHERE p.Id = {post.PowerStation} and v.enabled = 1
// order by 1;";
//var inv_sum = conn.Query<string>(sql).FirstOrDefault();
switch (post.FormType) switch (post.FormType)
{ {
@ -71,7 +88,7 @@ namespace SolarPower.Repository.Implement
sql = $@" sql = $@"
DROP TABLE IF EXISTS temp_inv; DROP TABLE IF EXISTS temp_inv;
create TEMPORARY TABLE temp_inv as create TEMPORARY TABLE temp_inv as
SELECT DATE_FORMAT(a.report_date,'%m-%d %H') report_date, {inv}, SELECT DATE_FORMAT(a.report_date,'%m-%d %H') report_date, {inv}
a.hourKWH hourKWH, a.hourKWHp 'hourKWHp', a.irradiance 'irradiance', a.Temperature 'temperature', a.hourKWH hourKWH, a.hourKWHp 'hourKWHp', a.irradiance 'irradiance', a.Temperature 'temperature',
a.hourmoney 'hourmoney', c.TODAYKWH 'totKWH', c.KWHKWP 'totKWHKWP', c.money 'totmoney', stationName, powerRate daymoney, c.SOLARHOUR tothour,round(a.PR, 2) as pr,GeneratingCapacity a.hourmoney 'hourmoney', c.TODAYKWH 'totKWH', c.KWHKWP 'totKWHKWP', c.money 'totmoney', stationName, powerRate daymoney, c.SOLARHOUR tothour,round(a.PR, 2) as pr,GeneratingCapacity
FROM report_invday a FROM report_invday a
@ -96,7 +113,7 @@ namespace SolarPower.Repository.Implement
-- step 3 -- step 3
select * from temp_inv select * from temp_inv
union union
SELECT '' report_date, {inv_sum}, SELECT '' report_date, {inv}
round(sum(hourKWH), 2) hourKWH, round(sum(hourKWHp), 2) hourKWHp, round(sum(irradiance), 2) irradiance, round(sum(hourKWH), 2) hourKWH, round(sum(hourKWHp), 2) hourKWHp, round(sum(irradiance), 2) irradiance,
avg(temperature) temperature, round(sum(hourmoney), 2) hourmoney, avg(totKWH) totKWH, avg(totKWHKWP) totKWHKWP, avg(totmoney) totmoney, '' stationName, avg(temperature) temperature, round(sum(hourmoney), 2) hourmoney, avg(totKWH) totKWH, avg(totKWHKWP) totKWHKWP, avg(totmoney) totmoney, '' stationName,
avg(daymoney) daymoney, avg(tothour) tothour, avg(pr) pr, avg(GeneratingCapacity) GeneratingCapacity avg(daymoney) daymoney, avg(tothour) tothour, avg(pr) pr, avg(GeneratingCapacity) GeneratingCapacity
@ -106,71 +123,115 @@ namespace SolarPower.Repository.Implement
case 1: //月報 case 1: //月報
if(post.SearchType == 2) if(post.SearchType == 2)
{ {
sql += @$" sql= @$" DROP TABLE IF EXISTS temp_inv;
SET @sql = NULL; create TEMPORARY TABLE temp_inv as
SELECT SELECT DATE_FORMAT(a.report_date,'%m/%d') report_date, {inv}
GROUP_CONCAT(DISTINCT b.TODAYKWH 'dayKWH', round((b.TODAYKWH / c.monthKWH)*100,2) 'dayKWHp', b.SOLARHOUR 'tothour', b.KWHKWP 'KWHKWP', b.PR,z.GeneratingCapacity,
CONCAT('max(case when INVERTERID = ''', INVERTERID, ''' then round(a.KWH, 2) end) ''inv_', right(INVERTERID, 2), '''') d.irradiance 'irradiance', d.Temperature 'temperature', b.money 'soldmoney',
) INTO @sql c.monthKWH 'monthKWH', c.money 'monthmoney', stationName, powerRate 'monthmoneyone',SolarType,SiteDB,d.IrrDay
FROM inverter_history_day where powerstationId = {post.PowerStation} and DATE_FORMAT(`TIMESTAMP`,'%Y-%m') = '{post.Time}'; FROM report_invday a left join
SET @sql = CONCAT('SELECT DATE_FORMAT(a.TIMESTAMP,''%m/%d'') report_date, ', @sql,
',b.TODAYKWH ''dayKWH'', round((b.TODAYKWH / c.monthKWH)*100,2) ''dayKWHp'', b.SOLARHOUR ''tothour'', b.KWHKWP ''KWHKWP'', b.PR,z.GeneratingCapacity,
d.irradiance ''irradiance'', d.Temperature ''temperature'', b.money ''soldmoney'',
c.monthKWH ''monthKWH'', c.money ''monthmoney'', stationName, powerRate ''monthmoneyone'',SolarType,SiteDB,d.IrrDay
FROM inverter_history_day a left join
( # inv ( # inv
select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') report_date, siteid, sitetype, #, round(KWH, 2) KWH, select powerStationid, DATE_FORMAT(TIMESTAMP,'%Y-%m-%d') report_date, siteid, sitetype, #, round(KWH, 2) KWH,
round(todayKWH, 2) todayKWH,round(KWHKWP, 2) KWHKWP, round(PR, 2) PR, ifnull(round(money, 2),0) money, round(SOLARHOUR, 2) SOLARHOUR round(todayKWH, 2) todayKWH,round(KWHKWP, 2) KWHKWP, round(PR, 2) PR, ifnull(round(money, 2),0) money, round(SOLARHOUR, 2) SOLARHOUR
from power_station_history_day from power_station_history_day
where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m'') = ''{post.Time}'' where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,'%Y-%m') = '{post.Time}'
) b on a.powerStationid = b.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') = b.report_date ) b on a.powerStationid = b.powerStationid and DATE_FORMAT(a.report_date,'%Y-%m-%d') = b.report_date
left join left join
( # month ( # month
select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y-%m'') report_date, sitetype, round(monthKWH, 2) monthKWH,round(KWHKWP, 2) KWHKWP select powerStationid, DATE_FORMAT(TIMESTAMP,'%Y-%m') report_date, sitetype, round(monthKWH, 2) monthKWH,round(KWHKWP, 2) KWHKWP
, round(PR, 2) PR, round(money, 2) money , round(SOLARHOUR, 2) SOLARHOUR , round(PR, 2) PR, round(money, 2) money , round(SOLARHOUR, 2) SOLARHOUR
from power_station_history_month from power_station_history_month
where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m'') = ''{post.Time}'' where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,'%Y-%m') = '{post.Time}'
) c on a.powerStationid = c.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m'') = c.report_date ) c on a.powerStationid = c.powerStationid and DATE_FORMAT(a.report_date,'%Y-%m') = c.report_date
left join left join
( (
select powerStationID, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'')report_date, irradiance, Temperature ,IrrDay select powerStationID, DATE_FORMAT(TIMESTAMP,'%Y-%m-%d')report_date, irradiance, Temperature ,IrrDay
from sensor_history_day from sensor_history_day
where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m'') = ''{post.Time}'' where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,'%Y-%m') = '{post.Time}'
) d on a.powerStationid = d.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') = d.report_date ) d on a.powerStationid = d.powerStationid and DATE_FORMAT(a.report_date,'%Y-%m-%d') = d.report_date
join join
( (
select id, name stationName, powerRate,SolarType,SiteDB,GeneratingCapacity from power_station where id = {post.PowerStation} select id, name stationName, powerRate,SolarType,SiteDB,GeneratingCapacity from power_station where id = {post.PowerStation}
)z on a.powerstationid = z.id )z on a.powerstationid = z.id
where DATE_FORMAT(a.TIMESTAMP,''%Y-%m'') = ''{post.Time}'' where a.PowerStationID ={post.PowerStation} and DATE_FORMAT(a.report_date,'%Y-%m') = '{post.Time}'
GROUP BY DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') GROUP BY DATE_FORMAT(a.report_date,'%Y-%m-%d')
order by DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') '); order by DATE_FORMAT(a.report_date,'%Y-%m-%d') ;
-- step 2
# select @sql as 'mySelect'; # DROP TABLE IF EXISTS temp_inv2;
PREPARE stmt FROM @sql; create TEMPORARY TABLE temp_inv2 as
EXECUTE stmt; select * from temp_inv;
DEALLOCATE PREPARE stmt;"; -- step 3
select * from temp_inv
union
SELECT '' report_date, {inv}
round(sum(dayKWH), 2) dayKWH, round(sum(dayKWHp), 2) dayKWHp, round(max(tothour), 2) tothour, avg(KWHKWP) KWHKWP, round(avg(PR), 2) PR,
GeneratingCapacity, round(sum(irradiance), 2) irradiance, avg(temperature) temperature, round(sum(soldmoney), 2) soldmoney,
monthKWH, monthmoney, stationName, monthmoneyone, SolarType, SiteDB, IrrDay
from temp_inv2 a; ";
} }
else else
{ //區間 { //區間
var times = post.Time.Replace('-', 'a').Replace('/', '-').Replace(" ", "").Split('a'); var times = post.Time.Replace('-', 'a').Replace('/', '-').Replace(" ", "").Split('a');
#region old sql
//sql = @$"SET @sql = NULL;
// SELECT
// GROUP_CONCAT(DISTINCT
// CONCAT('max(case when INVERTERID = ''', INVERTERID, ''' then round(a.KWH, 2) end) ''inv_', right(INVERTERID, 2), '''')
// ) INTO @sql
// FROM inverter_history_day where powerstationId = {post.PowerStation} and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') BETWEEN '{times[0]}' AND '{times[1]}';
//SET @sql = CONCAT('SELECT DATE_FORMAT(a.TIMESTAMP,''%m/%d'') report_date, ', @sql,
// ',b.TODAYKWH ''dayKWH'', round((b.TODAYKWH / c.monthKWH)*100,2) ''dayKWHp'', b.SOLARHOUR ''tothour'', b.KWHKWP ''KWHKWP'', b.PR,GeneratingCapacity,
// d.irradiance ''irradiance'', d.Temperature ''temperature'', b.money ''soldmoney'',
// c.monthKWH ''monthKWH'', c.money ''monthmoney'', stationName, powerRate ''monthmoneyone'',SolarType,SiteDB ,d.IrrDay
// FROM inverter_history_day a left join
// ( # 每日加總 inv
// select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') report_date, siteid, sitetype, #, round(KWH, 2) KWH,
// round(todayKWH, 2) todayKWH,round(KWHKWP, 2) KWHKWP, round(PR, 2) PR, ifnull(round(money, 2),0) money, round(SOLARHOUR, 2) SOLARHOUR
// from power_station_history_day
// where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') BETWEEN ''{times[0]}'' AND ''{times[1]}''
// ) b on a.powerStationid = b.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') = b.report_date
// left join
// ( # month
// SELECT powerStationid, ROUND(SUM(TODAYKWH),2) AS monthKWH,
// ROUND(AVG(KWHKWP),2) AS KWHKWP,
// ROUND(AVG(PR),2) AS PR,
// ROUND(SUM(MONEY),2) AS money,
// ROUND(SUM(SOLARHOUR),2) AS SOLARHOUR
// FROM power_station_history_day where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') BETWEEN ''{times[0]}'' AND ''{times[1]}''
// ) c on a.powerStationid = c.powerStationid
// left join
// (
// select powerStationID, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'')report_date, irradiance, Temperature ,IrrDay
// from sensor_history_day
// where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') BETWEEN ''{times[0]}'' AND ''{times[1]}''
// ) d on a.powerStationid = d.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') = d.report_date
// join
// (
// select id, name stationName, powerRate, SolarType, SiteDB,GeneratingCapacity from power_station where id = {post.PowerStation}
// )z on a.powerstationid = z.id
// where DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') BETWEEN ''{times[0]}'' AND ''{times[1]}''
// GROUP BY DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'')
// order by DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') ');
sql += @$"SET @sql = NULL; // # select @sql as 'mySelect'; #顯示動態語法
SELECT // PREPARE stmt FROM @sql;
GROUP_CONCAT(DISTINCT // EXECUTE stmt;
CONCAT('max(case when INVERTERID = ''', INVERTERID, ''' then round(a.KWH, 2) end) ''inv_', right(INVERTERID, 2), '''') // DEALLOCATE PREPARE stmt;";
) INTO @sql #endregion old sql
FROM inverter_history_day where powerstationId = {post.PowerStation} and DATE_FORMAT(`TIMESTAMP`,'%Y-%m-%d') BETWEEN '{times[0]}' AND '{times[1]}';
SET @sql = CONCAT('SELECT DATE_FORMAT(a.TIMESTAMP,''%m/%d'') report_date, ', @sql, sql = @$" DROP TABLE IF EXISTS temp_inv;
',b.TODAYKWH ''dayKWH'', round((b.TODAYKWH / c.monthKWH)*100,2) ''dayKWHp'', b.SOLARHOUR ''tothour'', b.KWHKWP ''KWHKWP'', b.PR,GeneratingCapacity, create TEMPORARY TABLE temp_inv as
d.irradiance ''irradiance'', d.Temperature ''temperature'', b.money ''soldmoney'', SELECT DATE_FORMAT(a.report_date,'%m/%d') report_date, {inv}
c.monthKWH ''monthKWH'', c.money ''monthmoney'', stationName, powerRate ''monthmoneyone'',SolarType,SiteDB ,d.IrrDay b.TODAYKWH 'dayKWH', round((b.TODAYKWH / c.monthKWH)*100,2) 'dayKWHp', b.SOLARHOUR 'tothour', b.KWHKWP 'KWHKWP', b.PR,GeneratingCapacity,
FROM inverter_history_day a left join d.irradiance 'irradiance', d.Temperature 'temperature', b.money 'soldmoney',
c.monthKWH 'monthKWH', c.money 'monthmoney', stationName, powerRate 'monthmoneyone',SolarType,SiteDB ,d.IrrDay
FROM report_invday a left join
( # inv ( # inv
select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') report_date, siteid, sitetype, #, round(KWH, 2) KWH, select powerStationid, DATE_FORMAT(TIMESTAMP,'%Y-%m-%d') report_date, siteid, sitetype, #, round(KWH, 2) KWH,
round(todayKWH, 2) todayKWH,round(KWHKWP, 2) KWHKWP, round(PR, 2) PR, ifnull(round(money, 2),0) money, round(SOLARHOUR, 2) SOLARHOUR round(todayKWH, 2) todayKWH,round(KWHKWP, 2) KWHKWP, round(PR, 2) PR, ifnull(round(money, 2),0) money, round(SOLARHOUR, 2) SOLARHOUR
from power_station_history_day from power_station_history_day
where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') BETWEEN ''{times[0]}'' AND ''{times[1]}'' where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,'%Y-%m-%d') BETWEEN '{times[0]}' AND '{times[1]}'
) b on a.powerStationid = b.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') = b.report_date ) b on a.powerStationid = b.powerStationid and DATE_FORMAT(a.report_date,'%Y-%m-%d') = b.report_date
left join left join
( # month ( # month
SELECT powerStationid, ROUND(SUM(TODAYKWH),2) AS monthKWH, SELECT powerStationid, ROUND(SUM(TODAYKWH),2) AS monthKWH,
@ -178,73 +239,127 @@ namespace SolarPower.Repository.Implement
ROUND(AVG(PR),2) AS PR, ROUND(AVG(PR),2) AS PR,
ROUND(SUM(MONEY),2) AS money, ROUND(SUM(MONEY),2) AS money,
ROUND(SUM(SOLARHOUR),2) AS SOLARHOUR ROUND(SUM(SOLARHOUR),2) AS SOLARHOUR
FROM power_station_history_day where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') BETWEEN ''{times[0]}'' AND ''{times[1]}'' FROM power_station_history_day where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,'%Y-%m-%d') BETWEEN '{times[0]}' AND '{times[1]}'
) c on a.powerStationid = c.powerStationid ) c on a.powerStationid = c.powerStationid
left join left join
( (
select powerStationID, DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'')report_date, irradiance, Temperature ,IrrDay select powerStationID, DATE_FORMAT(TIMESTAMP,'%Y-%m-%d')report_date, irradiance, Temperature ,IrrDay
from sensor_history_day from sensor_history_day
where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') BETWEEN ''{times[0]}'' AND ''{times[1]}'' where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,'%Y-%m-%d') BETWEEN '{times[0]}' AND '{times[1]}'
) d on a.powerStationid = d.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') = d.report_date ) d on a.powerStationid = d.powerStationid and DATE_FORMAT(a.report_date,'%Y-%m-%d') = d.report_date
join join
( (
select id, name stationName, powerRate, SolarType, SiteDB,GeneratingCapacity from power_station where id = {post.PowerStation} select id, name stationName, powerRate, SolarType, SiteDB,GeneratingCapacity from power_station where id = {post.PowerStation}
)z on a.powerstationid = z.id )z on a.powerstationid = z.id
where DATE_FORMAT(TIMESTAMP,''%Y-%m-%d'') BETWEEN ''{times[0]}'' AND ''{times[1]}'' where a.powerstationId = {post.PowerStation} and DATE_FORMAT(a.report_date,'%Y-%m-%d') BETWEEN '{times[0]}' AND '{times[1]}'
GROUP BY DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') GROUP BY DATE_FORMAT(a.report_date,'%Y-%m-%d')
order by DATE_FORMAT(a.TIMESTAMP,''%Y-%m-%d'') '); order by DATE_FORMAT(a.report_date,'%Y-%m-%d');
-- step 2
# select @sql as 'mySelect'; # DROP TABLE IF EXISTS temp_inv2;
PREPARE stmt FROM @sql; create TEMPORARY TABLE temp_inv2 as
EXECUTE stmt; select * from temp_inv;
DEALLOCATE PREPARE stmt;"; -- step 3
select * from temp_inv
union
SELECT '' report_date, {inv}
round(sum(dayKWH), 2) dayKWH, round(sum(dayKWHp), 2) dayKWHp, round(max(tothour), 2) tothour, avg(KWHKWP) KWHKWP, round(avg(PR), 2) PR,
GeneratingCapacity, round(sum(irradiance), 2) irradiance, avg(temperature) temperature, round(sum(soldmoney), 2) soldmoney,
monthKWH, monthmoney, stationName, monthmoneyone, SolarType, SiteDB, IrrDay
from temp_inv2 a; ";
} }
break; break;
case 3: //年報 case 3: //年報
sql += @$" #region old SQL
SET @sql = NULL; //sql = @$"
SELECT // SET @sql = NULL;
GROUP_CONCAT(DISTINCT // SELECT
CONCAT('max(case when INVERTERID = ''', INVERTERID, ''' then round(a.KWH, 2) end) ''inv_', right(INVERTERID, 2), '''') // GROUP_CONCAT(DISTINCT
) INTO @sql // CONCAT('max(case when INVERTERID = ''', INVERTERID, ''' then round(a.KWH, 2) end) ''inv_', right(INVERTERID, 2), '''')
FROM inverter_history_month where powerstationId = {post.PowerStation} and DATE_FORMAT(`TIMESTAMP`,'%Y') = '{post.Time}'; // ) INTO @sql
SET @sql = CONCAT('SELECT DATE_FORMAT(a.TIMESTAMP,''%Y/%m'') report_date, ', @sql, // FROM inverter_history_month where powerstationId = {post.PowerStation} and DATE_FORMAT(`TIMESTAMP`,'%Y') = '{post.Time}';
',b.todayKWH ''dayKWH'', round((b.todayKWH / c.monthKWH)*100,2) ''dayKWHp'', b.SOLARHOUR ''tothour'', b.KWHKWP ''KWHKWP'', b.PR,z.GeneratingCapacity, // SET @sql = CONCAT('SELECT DATE_FORMAT(a.TIMESTAMP,''%Y/%m'') report_date, ', @sql,
d.irradiance ''irradiance'', d.Temperature ''temperature'', b.money ''soldmoney'', // ',b.todayKWH ''dayKWH'', round((b.todayKWH / c.monthKWH)*100,2) ''dayKWHp'', b.SOLARHOUR ''tothour'', b.KWHKWP ''KWHKWP'', b.PR,z.GeneratingCapacity,
c.monthKWH ''monthKWH'', c.money ''monthmoney'', stationName, powerRate ''monthmoneyone'',SolarType,SiteDB // d.irradiance ''irradiance'', d.Temperature ''temperature'', b.money ''soldmoney'',
FROM inverter_history_month a left join // c.monthKWH ''monthKWH'', c.money ''monthmoney'', stationName, powerRate ''monthmoneyone'',SolarType,SiteDB
// FROM inverter_history_month a left join
// ( # 每日加總 inv
// select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y-%m'') report_date, siteid, sitetype,
// round(MONTHKWH, 2) todayKWH,round(KWHKWP, 2) KWHKWP, round(PR, 2) PR, ifnull(round(money, 2),0) money, round(SOLARHOUR, 2) SOLARHOUR
// from power_station_history_month
// where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y'') = ''{post.Time}''
// ) b on a.powerStationid = b.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m'') = b.report_date
// left join
// ( # month
// select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y'') report_date, sitetype, sum(round(monthKWH, 2)) monthKWH , avg(round(KWHKWP, 2)) KWHKWP
// , avg(round(PR, 2)) PR, sum(round(money, 2)) money , sum(round(SOLARHOUR, 2)) SOLARHOUR
// from power_station_history_month
// where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y'') = ''{post.Time}'' group by DATE_FORMAT(TIMESTAMP,''%Y'')
// ) c on a.powerStationid = c.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y'') = c.report_date
// left join
// (
// select powerStationID, DATE_FORMAT(TIMESTAMP,''%Y-%m'')report_date, irradiance, Temperature
// from sensor_history_month
// where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y'') = ''{post.Time}''
// ) d on a.powerStationid = d.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m'') = d.report_date
// join
// (
// select id, name stationName, powerRate,SolarType,SiteDB,GeneratingCapacity from power_station where id = {post.PowerStation}
// )z on a.powerstationid = z.id
// where DATE_FORMAT(a.TIMESTAMP,''%Y'') = ''{post.Time}''
// GROUP BY DATE_FORMAT(a.TIMESTAMP,''%Y-%m'')
// order by DATE_FORMAT(a.TIMESTAMP,''%Y-%m'') ');
// # select @sql as 'mySelect'; #顯示動態語法
// PREPARE stmt FROM @sql;
// EXECUTE stmt;
// DEALLOCATE PREPARE stmt;";
#endregion
sql = @$" DROP TABLE IF EXISTS temp_inv;
create TEMPORARY TABLE temp_inv as
SELECT DATE_FORMAT(a.report_date,'%Y/%m') report_date, {inv}
b.todayKWH 'dayKWH', round((b.todayKWH / c.monthKWH)*100,2) 'dayKWHp', b.SOLARHOUR 'tothour', b.KWHKWP 'KWHKWP', b.PR,z.GeneratingCapacity,
d.irradiance 'irradiance', d.Temperature 'temperature', b.money 'soldmoney',
c.monthKWH 'monthKWH', c.money 'monthmoney', stationName, powerRate 'monthmoneyone',SolarType,SiteDB, IrrDay
FROM report_invday a left join
( # inv ( # inv
select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y-%m'') report_date, siteid, sitetype, select powerStationid, DATE_FORMAT(TIMESTAMP,'%Y-%m') report_date, siteid, sitetype,
round(MONTHKWH, 2) todayKWH,round(KWHKWP, 2) KWHKWP, round(PR, 2) PR, ifnull(round(money, 2),0) money, round(SOLARHOUR, 2) SOLARHOUR round(MONTHKWH, 2) todayKWH,round(KWHKWP, 2) KWHKWP, round(PR, 2) PR, ifnull(round(money, 2),0) money, round(SOLARHOUR, 2) SOLARHOUR
from power_station_history_month from power_station_history_month
where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y'') = ''{post.Time}'' where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,'%Y') = '{post.Time}'
) b on a.powerStationid = b.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m'') = b.report_date ) b on a.powerStationid = b.powerStationid and DATE_FORMAT(a.report_date,'%Y-%m') = b.report_date
left join left join
( # month ( # month
select powerStationid, DATE_FORMAT(TIMESTAMP,''%Y'') report_date, sitetype, sum(round(monthKWH, 2)) monthKWH , avg(round(KWHKWP, 2)) KWHKWP select powerStationid, DATE_FORMAT(TIMESTAMP,'%Y') report_date, sitetype, sum(round(monthKWH, 2)) monthKWH , avg(round(KWHKWP, 2)) KWHKWP
, avg(round(PR, 2)) PR, sum(round(money, 2)) money , sum(round(SOLARHOUR, 2)) SOLARHOUR , avg(round(PR, 2)) PR, sum(round(money, 2)) money , sum(round(SOLARHOUR, 2)) SOLARHOUR
from power_station_history_month from power_station_history_month
where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y'') = ''{post.Time}'' group by DATE_FORMAT(TIMESTAMP,''%Y'') where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,'%Y') = '{post.Time}' group by DATE_FORMAT(TIMESTAMP,'%Y')
) c on a.powerStationid = c.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y'') = c.report_date ) c on a.powerStationid = c.powerStationid and DATE_FORMAT(a.report_date,'%Y') = c.report_date
left join left join
( (
select powerStationID, DATE_FORMAT(TIMESTAMP,''%Y-%m'')report_date, irradiance, Temperature select powerStationID, DATE_FORMAT(TIMESTAMP,'%Y-%m')report_date, irradiance, Temperature, IrrDay
from sensor_history_month from sensor_history_month
where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,''%Y'') = ''{post.Time}'' where powerstationid = {post.PowerStation} and DATE_FORMAT(TIMESTAMP,'%Y') = '{post.Time}'
) d on a.powerStationid = d.powerStationid and DATE_FORMAT(a.TIMESTAMP,''%Y-%m'') = d.report_date ) d on a.powerStationid = d.powerStationid and DATE_FORMAT(a.report_date,'%Y-%m') = d.report_date
join join
( (
select id, name stationName, powerRate,SolarType,SiteDB,GeneratingCapacity from power_station where id = {post.PowerStation} select id, name stationName, powerRate,SolarType,SiteDB,GeneratingCapacity from power_station where id = {post.PowerStation}
)z on a.powerstationid = z.id )z on a.powerstationid = z.id
where DATE_FORMAT(a.TIMESTAMP,''%Y'') = ''{post.Time}'' where a.powerstationId = {post.PowerStation} and DATE_FORMAT(a.report_date,'%Y') = '{post.Time}'
GROUP BY DATE_FORMAT(a.TIMESTAMP,''%Y-%m'') GROUP BY DATE_FORMAT(a.report_date,'%Y-%m')
order by DATE_FORMAT(a.TIMESTAMP,''%Y-%m'') '); order by DATE_FORMAT(a.report_date,'%Y-%m') ;
-- step 2
# select @sql as 'mySelect'; # DROP TABLE IF EXISTS temp_inv2;
PREPARE stmt FROM @sql; create TEMPORARY TABLE temp_inv2 as
EXECUTE stmt; select * from temp_inv;
DEALLOCATE PREPARE stmt;"; -- step 3
select * from temp_inv
union
SELECT '' report_date, {inv}
round(sum(dayKWH), 2) dayKWH, round(sum(dayKWHp), 2) dayKWHp, round(max(tothour), 2) tothour, avg(KWHKWP) KWHKWP, round(avg(PR), 2) PR,
GeneratingCapacity, round(sum(irradiance), 2) irradiance, avg(temperature) temperature, round(sum(soldmoney), 2) soldmoney,
monthKWH, monthmoney, stationName, monthmoneyone, SolarType, SiteDB, IrrDay
from temp_inv2 a; ";
break; break;
} }