Browse Source

1、能耗数据同比环比接口编写;

dev
mh 5 months ago
parent
commit
3d6bc69b48
  1. 40
      user-service/src/main/java/com/mh/user/controller/EnergyDataController.java
  2. 167
      user-service/src/main/java/com/mh/user/dto/EnergyMomYoyDataDTO.java
  3. 25
      user-service/src/main/java/com/mh/user/mapper/EnergyDataMapper.java
  4. 94
      user-service/src/main/java/com/mh/user/mapper/provider/EnergyDataProvider.java
  5. 4
      user-service/src/main/java/com/mh/user/service/EnergyDataService.java
  6. 97
      user-service/src/main/java/com/mh/user/service/impl/EnergyDataServiceImpl.java
  7. 22
      user-service/src/test/java/com/mh/user/DealDataTest.java
  8. 37
      user-service/src/test/java/com/mh/user/TestJwtUtils.java
  9. 255
      能耗同比.sql
  10. 239
      能耗环比.sql

40
user-service/src/main/java/com/mh/user/controller/EnergyDataController.java

@ -1,7 +1,10 @@
package com.mh.user.controller; package com.mh.user.controller;
import com.github.pagehelper.PageInfo;
import com.mh.common.http.HttpResult; import com.mh.common.http.HttpResult;
import com.mh.common.page.PageResult;
import com.mh.user.annotation.SysLogger; import com.mh.user.annotation.SysLogger;
import com.mh.user.dto.EnergyMomYoyDataDTO;
import com.mh.user.entity.DeviceInstallEntity; import com.mh.user.entity.DeviceInstallEntity;
import com.mh.user.entity.EnergyDataEntity; import com.mh.user.entity.EnergyDataEntity;
import com.mh.user.entity.EnergyEntity; import com.mh.user.entity.EnergyEntity;
@ -38,4 +41,41 @@ public class EnergyDataController {
} }
@SysLogger(value="中央空调",optDesc = "按小时、天、月、年查询用水用电eer环比")
@PostMapping("/mom")
public HttpResult queryMomEnergyData(@RequestBody EnergyModel energyModel){
try{
String projectId=energyModel.getProjectId();
if(projectId!=null && projectId.length()>0){
PageResult result = energyDataService.queryMomEnergyData(energyModel);
return HttpResult.ok(result);
}else{
return HttpResult.error();
}
}catch (Exception e){
// e.printStackTrace();
return HttpResult.error("查询出错!");
}
}
@SysLogger(value="中央空调",optDesc = "按小时、天、月、年查询用水用电eer同比")
@PostMapping("/yoy")
public HttpResult queryYoyEnergyData(@RequestBody EnergyModel energyModel){
try{
String projectId=energyModel.getProjectId();
if(projectId!=null && projectId.length()>0){
List<EnergyDataEntity> list=energyDataService.queryEnergyData(energyModel);
int count=energyDataService.getEnergyDataCount(energyModel);
return HttpResult.ok(count,list);
}else{
return HttpResult.error();
}
}catch (Exception e){
// e.printStackTrace();
return HttpResult.error("查询出错!");
}
}
} }

167
user-service/src/main/java/com/mh/user/dto/EnergyMomYoyDataDTO.java

@ -0,0 +1,167 @@
package com.mh.user.dto;
import java.math.BigDecimal;
public class EnergyMomYoyDataDTO {
private String curDate;
private BigDecimal elect;
private BigDecimal cl;
private BigDecimal cop;
private BigDecimal lastElect;
private BigDecimal lastCl;
private BigDecimal lastCop;
private String electRatio;
private String clRatio;
private String copRatio;
private String projectId;
private String projectName;
private String ambTemp;
private String humidity;
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getAmbTemp() {
return ambTemp;
}
public void setAmbTemp(String ambTemp) {
this.ambTemp = ambTemp;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
public String getCurDate() {
return curDate;
}
public void setCurDate(String curDate) {
this.curDate = curDate;
}
public BigDecimal getElect() {
return elect;
}
public void setElect(BigDecimal elect) {
this.elect = elect;
}
public BigDecimal getCl() {
return cl;
}
public void setCl(BigDecimal cl) {
this.cl = cl;
}
public BigDecimal getCop() {
return cop;
}
public void setCop(BigDecimal cop) {
this.cop = cop;
}
public BigDecimal getLastElect() {
return lastElect;
}
public void setLastElect(BigDecimal lastElect) {
this.lastElect = lastElect;
}
public BigDecimal getLastCl() {
return lastCl;
}
public void setLastCl(BigDecimal lastCl) {
this.lastCl = lastCl;
}
public BigDecimal getLastCop() {
return lastCop;
}
public void setLastCop(BigDecimal lastCop) {
this.lastCop = lastCop;
}
public String getElectRatio() {
return electRatio;
}
public void setElectRatio(String electRatio) {
this.electRatio = electRatio;
}
public String getClRatio() {
return clRatio;
}
public void setClRatio(String clRatio) {
this.clRatio = clRatio;
}
public String getCopRatio() {
return copRatio;
}
public void setCopRatio(String copRatio) {
this.copRatio = copRatio;
}
public String getProjectId() {
return projectId;
}
public void setProjectId(String projectId) {
this.projectId = projectId;
}
@Override
public String toString() {
return "EnergyMomYoyDataDTO{" +
"curDate='" + curDate + '\'' +
", elect=" + elect +
", cl=" + cl +
", cop=" + cop +
", lastElect=" + lastElect +
", lastCl=" + lastCl +
", lastCop=" + lastCop +
", electRatio='" + electRatio + '\'' +
", clRatio='" + clRatio + '\'' +
", copRatio='" + copRatio + '\'' +
", projectId='" + projectId + '\'' +
", projectName='" + projectName + '\'' +
", ambTemp='" + ambTemp + '\'' +
", humidity='" + humidity + '\'' +
'}';
}
}

25
user-service/src/main/java/com/mh/user/mapper/EnergyDataMapper.java

@ -1,6 +1,7 @@
package com.mh.user.mapper; package com.mh.user.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mh.user.dto.EnergyMomYoyDataDTO;
import com.mh.user.entity.EnergyDataEntity; import com.mh.user.entity.EnergyDataEntity;
import com.mh.user.mapper.provider.EnergyDataProvider; import com.mh.user.mapper.provider.EnergyDataProvider;
import org.apache.ibatis.annotations.*; import org.apache.ibatis.annotations.*;
@ -91,4 +92,28 @@ public interface EnergyDataMapper extends BaseMapper<EnergyDataEntity> {
@Param("endDate") String endDate, @Param("endDate") String endDate,
@Param("queryType") int queryType); @Param("queryType") int queryType);
@SelectProvider(type = EnergyDataProvider.class, method = "queryMomEnergyHourAndDayData")
@Results(id = "mom_yoy", value = {
@Result(property = "curDate", column = "cur_date"),
@Result(property = "projectId", column = "project_id"),
@Result(property = "projectName", column = "project_name"),
@Result(property = "ambTemp", column = "amb_Temp"),
@Result(property = "humidity", column = "humidity"),
@Result(property = "elect", column = "elect"),
@Result(property = "cl", column = "cl"),
@Result(property = "cop", column = "cop"),
@Result(property = "last_elect", column = "lastElect"),
@Result(property = "last_cl", column = "lastCl"),
@Result(property = "last_cop", column = "lastCop"),
@Result(property = "elect_ratio", column = "electRatio"),
@Result(property = "cl_ratio", column = "clRatio"),
@Result(property = "cop_ratio", column = "copRatio")
})
List<EnergyMomYoyDataDTO> queryMomEnergyHourAndDayData(@Param("tableName") String tableName,
@Param("projectId") String projectId,
@Param("startDate") String startDate,
@Param("endDate") String endDate,
@Param("lastStartDate") String lastStartDate,
@Param("lastEndDate") String lastEndDate,
@Param("dateType") String dateType);
} }

94
user-service/src/main/java/com/mh/user/mapper/provider/EnergyDataProvider.java

@ -1,5 +1,9 @@
package com.mh.user.mapper.provider; package com.mh.user.mapper.provider;
import org.apache.ibatis.annotations.Param;
import java.util.Map;
public class EnergyDataProvider { public class EnergyDataProvider {
//按十分钟 //按十分钟
@ -307,4 +311,94 @@ public class EnergyDataProvider {
// System.out.println(sql.toString()); // System.out.println(sql.toString());
return sql.toString(); return sql.toString();
} }
public String queryMomEnergyHourAndDayData(Map<String, Object> params) {
StringBuilder sql = new StringBuilder();
sql.append("select " +
" t1.cur_date, " +
" t1.elect, " +
" t1.cl, " +
" t1.cop, " +
" t1.project_id, " +
" pi2.project_name, " +
" 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 elect_ratio, " +
" case " +
" when t2.cl > 0 then concat(convert(decimal(18, 2),(t1.cl - t2.cl)/ t2.cl * 100), '%') " +
" else '上期无数据' " +
" end cl_ratio, " +
" case " +
" when t2.cop > 0 then concat(convert(decimal(18, 2),(t1.cop - t2.cop)/ t2.cop * 100), '%') " +
" else '上期无数据' " +
" end cop_ratio, " +
" ht.amb_temp, " +
" ht.humidity " +
"from " +
" ( " +
" select " +
" cur_date, " +
" elect, " +
" cl, " +
" cop, " +
" project_id, " +
" convert(datetime, ");
if ("day".equals(params.get("dateType"))) {
sql.append(" concat(cur_date, ' 00:00:00')) as change_cur_date ");
} else if ("hour".equals(params.get("dateType"))) {
sql.append(" concat(cur_date, ':00:00')) as change_cur_date ");
}
sql.append(
" from " +
" ${tableName} " +
" where " +
" cur_date >= #{startDate} " +
" and cur_date <= #{endDate} " +
" and project_id = #{projectId} " +
" ) t1 " +
"left join " +
" ( " +
" select " +
" cur_date, " +
" elect, " +
" cl, " +
" cop, " +
" project_id, " +
" convert(datetime, ");
if ("day".equals(params.get("dateType"))) {
sql.append(" concat(cur_date, ' 00:00:00')) as change_cur_date ");
} else if ("hour".equals(params.get("dateType"))) {
sql.append(" concat(cur_date, ':00:00')) as change_cur_date ");
}
sql.append(
" from " +
" ${tableName} " +
" where " +
" cur_date >= #{lastStartDate} " +
" and cur_date <= #{lastEndDate} " +
" and project_id = 3 " +
" ) t2 " +
" on " +
" t1.change_cur_date = dateadd(${dateType}, 1, t2.change_cur_date) " +
"left join hour_temperature ht " +
"on " +
" ht.cur_date = t1.change_cur_date " +
"left join ( " +
" select " +
" id, " +
" project_name " +
" from " +
" project_info " +
" where " +
" id = #{projectId}) pi2 " +
"on " +
" pi2.id = t1.project_id " +
"order by " +
" t1.cur_date ");
return sql.toString();
}
} }

4
user-service/src/main/java/com/mh/user/service/EnergyDataService.java

@ -1,5 +1,8 @@
package com.mh.user.service; package com.mh.user.service;
import com.github.pagehelper.PageInfo;
import com.mh.common.page.PageResult;
import com.mh.user.dto.EnergyMomYoyDataDTO;
import com.mh.user.entity.EnergyDataEntity; import com.mh.user.entity.EnergyDataEntity;
import com.mh.user.model.EnergyModel; import com.mh.user.model.EnergyModel;
import java.util.List; import java.util.List;
@ -10,4 +13,5 @@ public interface EnergyDataService {
int getEnergyDataCount(EnergyModel energyModel); int getEnergyDataCount(EnergyModel energyModel);
PageResult queryMomEnergyData(EnergyModel energyModel);
} }

97
user-service/src/main/java/com/mh/user/service/impl/EnergyDataServiceImpl.java

@ -1,5 +1,12 @@
package com.mh.user.service.impl; package com.mh.user.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mh.common.page.MybatisPageHelper;
import com.mh.common.page.PageRequest;
import com.mh.common.page.PageResult;
import com.mh.common.utils.StringUtils;
import com.mh.user.dto.EnergyMomYoyDataDTO;
import com.mh.user.entity.EnergyDataEntity; import com.mh.user.entity.EnergyDataEntity;
import com.mh.user.mapper.EnergyDataMapper; import com.mh.user.mapper.EnergyDataMapper;
import com.mh.user.model.EnergyModel; import com.mh.user.model.EnergyModel;
@ -7,6 +14,10 @@ import com.mh.user.service.EnergyDataService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.List; import java.util.List;
@Service @Service
@ -63,4 +74,90 @@ public class EnergyDataServiceImpl implements EnergyDataService {
} }
} }
@Override
public PageResult queryMomEnergyData(EnergyModel energyModel) {
String projectId=energyModel.getProjectId();
String startDate=energyModel.getStartDate();
String endDate=energyModel.getEndDate();
int page=energyModel.getPage();
int limit=energyModel.getLimit();
int type=energyModel.getType();
int queryType=energyModel.getQueryType();
String lastStartDate = "";
String lastEndDate = "";
String tableName = "energy_data_hour";
String dateType = "hour";
if (StringUtils.isBlank(endDate)) {
endDate = startDate;
}
switch (type) {
case 1:
// 按十分钟查询
break;
case 2:
// 按小时查询
startDate = startDate + " 00";
endDate = endDate + " 23";
// 定义日期时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
// startDate 减一个小时
LocalDateTime startDateTime = LocalDateTime.parse(startDate, formatter);
LocalDateTime newStartDateTime = startDateTime.minusHours(1);
lastStartDate = newStartDateTime.format(formatter);
// endDate 减一个小时
LocalDateTime endDateTime = LocalDateTime.parse(endDate, formatter);
LocalDateTime newEndDateTime = endDateTime.minusHours(1);
lastEndDate = newEndDateTime.format(formatter);
break;
case 3:
tableName = "energy_data_day";
dateType = "day";
// 按天查询
YearMonth yearMonthStart = YearMonth.parse(startDate, DateTimeFormatter.ofPattern("yyyy-MM"));
LocalDate firstDayOfMonth = yearMonthStart.atDay(1);
LocalDate lastDayOfMonth = yearMonthStart.atEndOfMonth();
startDate = firstDayOfMonth.toString();
endDate = lastDayOfMonth.toString();
// 定义日期时间格式
DateTimeFormatter dayMatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// startDate 减一天
LocalDateTime startDayDate = LocalDateTime.parse(startDate + "T00:00:00", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
LocalDateTime newStartDayDate = startDayDate.minusDays(1);
lastStartDate = newStartDayDate.format(dayMatter);
// endDate 减一天
LocalDateTime endDayDate = LocalDateTime.parse(endDate + "T23:59:59", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
LocalDateTime newEndDayDate = endDayDate.minusDays(1);
if (newEndDayDate.getDayOfMonth() == 1) {
newEndDayDate = newEndDayDate.minusDays(1).withHour(23).withMinute(59).withSecond(59);
}
lastEndDate = newEndDayDate.format(dayMatter);
default:
break;
}
PageRequest pageRequest = new PageRequest();
if (page != 0) {
PageHelper.startPage(page, limit);
pageRequest.setPageNum(page);
pageRequest.setPageSize(limit);
}
List<EnergyMomYoyDataDTO> resultList = energyDataMapper.queryMomEnergyHourAndDayData(tableName,
projectId,
startDate,
endDate,
lastStartDate,
lastEndDate,
dateType);
return MybatisPageHelper.getPageResult(pageRequest, new PageInfo<>(resultList));
}
} }

22
user-service/src/test/java/com/mh/user/DealDataTest.java

@ -1,6 +1,11 @@
package com.mh.user; package com.mh.user;
import com.github.pagehelper.PageInfo;
import com.mh.common.page.PageResult;
import com.mh.user.dto.EnergyMomYoyDataDTO;
import com.mh.user.mapper.chillers.DealDataMapper; import com.mh.user.mapper.chillers.DealDataMapper;
import com.mh.user.model.EnergyModel;
import com.mh.user.service.EnergyDataService;
import com.mh.user.service.chillers.DealDataService; import com.mh.user.service.chillers.DealDataService;
import com.mh.user.utils.ExchangeStringUtil; import com.mh.user.utils.ExchangeStringUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -42,4 +47,21 @@ public class DealDataTest extends UserServiceApplicationTests {
log.error("能效监测定时汇总异常", e); log.error("能效监测定时汇总异常", e);
} }
} }
@Autowired
private EnergyDataService energyDataService;
@Test
public void queryEnergyData(){
EnergyModel energyModel = new EnergyModel();
energyModel.setPage(1);
energyModel.setLimit(10);
energyModel.setStartDate("2024-07");
energyModel.setProjectId("3");
energyModel.setQueryType(0);
energyModel.setType(3);
PageResult pageResult = energyDataService.queryMomEnergyData(energyModel);
System.out.println(pageResult);
}
} }

37
user-service/src/test/java/com/mh/user/TestJwtUtils.java

@ -6,6 +6,8 @@ import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts; import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.SignatureAlgorithm;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -154,14 +156,33 @@ public class TestJwtUtils {
// System.out.println("name:" + name); // System.out.println("name:" + name);
// //
// } // }
String receiveData = "58 03 04 E8 FF 00 10 37 6A".replaceAll(" ", ""); // String receiveData = "58 03 04 E8 FF 00 10 37 6A".replaceAll(" ", "");
String data = receiveData.substring(receiveData.length() - 8, receiveData.length() - 6) // String data = receiveData.substring(receiveData.length() - 8, receiveData.length() - 6)
+ receiveData.substring(receiveData.length() - 6, receiveData.length() - 4) // + receiveData.substring(receiveData.length() - 6, receiveData.length() - 4)
+ receiveData.substring(receiveData.length() - 12, receiveData.length() - 10) // + receiveData.substring(receiveData.length() - 12, receiveData.length() - 10)
+ receiveData.substring(receiveData.length() - 10, receiveData.length() - 8); // + receiveData.substring(receiveData.length() - 10, receiveData.length() - 8);
System.out.println(data); // System.out.println(data);
float c4FBC834 = ExchangeStringUtil.hexToSingle("C4FBC834"); // float c4FBC834 = ExchangeStringUtil.hexToSingle("C4FBC834");
System.out.println(c4FBC834); // System.out.println(c4FBC834);
// 原始字符串
String dateString = "2024-11-22 00";
// 定义日期时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
// 将字符串转换为 LocalDateTime
LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
// 减去一个小时
LocalDateTime newDateTime = dateTime.minusHours(1);
// 将新的 LocalDateTime 转换为字符串
String newDateTimeString = newDateTime.format(formatter);
// 输出结果
System.out.println("Original Date and Time: " + dateString);
System.out.println("New Date and Time: " + newDateTimeString);
} }
} }

255
能耗同比.sql

@ -0,0 +1,255 @@
-- 小时同比
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

239
能耗环比.sql

@ -0,0 +1,239 @@
-- 小时环比
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
Loading…
Cancel
Save