비육돈사
AVERAGE_WEIGHT - 돼지 평균 몸무게
Temp_Out - TEMP_WEATHER_1(63) - 외부온도
EC_P – EC_MAN_1(9) -분뇨 EC
CO2 – C_DOL_1(3) - 내부 이산화탄소 농도
Ventilation rate - VR_1(13), VR_2(14) – 환기율
RH_Out – RH_WEATHER_1(64) - 외부 습도
NH3 – AMM_DOL_1(4) - 내부 암모니아 농도
Temp – TEM_DOL_1(1) - 내부 온도
RH – RH_DOL_1(2)- 내부 습도
Temp_P – TEMP_MAN_1(7) -  분뇨 온도
PH_P – PH_MAN_1(8) - 분뇨 PH


자돈사
AVERAGE_WEIGHT - 돼지 평균 몸무게
Temp_Out - TEMP_WEATHER_1(63) - 외부온도
EC_P – EC_MAN_1(23) -분뇨 EC
CO2 – C_DOL_1(17) - 내부 이산화탄소 농도
Ventilation rate - VR_1(27), VR_2(28) – 환기율
RH_Out – RH_WEATHER_1(64) - 외부 습도
NH3 – AMM_DOL_1(18) - 내부 암모니아 농도
Temp – TEM_DOL_1(15) - 내부 온도
RH – RH_DOL_1(16)- 내부 습도
Temp_P – TEMP_MAN_1(21) -  분뇨 온도
PH_P – PH_MAN_1(22) - 분뇨 PH
비육돈사, 자돈사 모두 메탄 센서는 ‘CH4 - MET_1(203) – 내부 메탄 농도’ 로 사용하시면 됩니다.


--
-- Common
--
1.DB tables: sensor_info_yyyymm
- tables are decided by simulation from~to condition (PERIOD_FR~PERIOD_TO)
- if simulation period includes multiple yyyymm, use UNION ALL

2.VR 
  자돈사   : ((SQRT(2*NVL(VR1,0)/1.3)*0.096) + (SQRT(2*NVL(VR2,0)/1.3)*0.126))/76.43097*60
  비육돈사 : ((SQRT(2*NVL(VR1,0)/1.3)*0.096) + (SQRT(2*NVL(VR2,0)/1.3)*0.126))/119.04057*60

--
-- if 축사선택(select house) is selected with 비육돈사 (porker house)
--
SELECT S.create_dt
       ,ROUND(W.AVERAGE_WEIGHT,2) AS AVERAGE_WEIGHT
       ,ROUND(F.FEED_WEIGHT,2) AS FEED_WEIGHT
       ,S.Temp_Out
       ,S.EC_P
       ,S.CO2
       ,ROUND(((SQRT(2*NVL(VR1,0)/1.3)*0.096) + (SQRT(2*NVL(VR2,0)/1.3)*0.126))/119.04057*60, 2) AS "Ventilation rate"
       ,S.RH_Out
       ,S.NH3
       ,S.Temp
       ,S.RH
       ,S.Temp_P
       ,S.PH_P
       ,S.CH4
  FROM (
      SELECT timestamp AS create_dt
             ,ROUND(NVL(MAX(IF(sensor_id=63, sensor_value, NULL)),0), 2) AS Temp_Out
             ,ROUND(MAX(IF(sensor_id=9, sensor_value, NULL)), 2) AS EC_P
             ,ROUND(MAX(IF(sensor_id=3, sensor_value, NULL)), 2) AS CO2
             ,ROUND(MAX(IF(sensor_id=13, sensor_value, NULL)), 2) AS VR1
             ,ROUND(MAX(IF(sensor_id=14, sensor_value, NULL)), 2) AS VR2
             ,ROUND(MAX(IF(sensor_id=64, sensor_value, NULL)), 2) AS RH_Out
             ,ROUND(MAX(IF(sensor_id=4, sensor_value, NULL)), 2) AS NH3
             ,ROUND(MAX(IF(sensor_id=1, sensor_value, NULL)), 2) AS Temp
             ,ROUND(MAX(IF(sensor_id=2, sensor_value, NULL)), 2) AS RH
             ,ROUND(MAX(IF(sensor_id=7, sensor_value, NULL)), 2) AS Temp_P
             ,ROUND(MAX(IF(sensor_id=8, sensor_value, NULL)), 2) AS PH_P
             ,ROUND(MAX(IF(sensor_id=203, sensor_value, NULL)), 2) AS CH4
       FROM sensor_info_202409
      WHERE DATE_FORMAT(timestamp, '%Y%m%d') BETWEEN '20240901' AND '20241014'
      GROUP BY timestamp
      UNION ALL
      SELECT timestamp AS create_dt
             ,ROUND(NVL(MAX(IF(sensor_id=63, sensor_value, NULL)),0), 2) AS Temp_Out
             ,ROUND(MAX(IF(sensor_id=9, sensor_value, NULL)), 2) AS EC_P
             ,ROUND(MAX(IF(sensor_id=3, sensor_value, NULL)), 2) AS CO2
             ,ROUND(MAX(IF(sensor_id=13, sensor_value, NULL)), 2) AS VR1
             ,ROUND(MAX(IF(sensor_id=14, sensor_value, NULL)), 2) AS VR2
             ,ROUND(MAX(IF(sensor_id=64, sensor_value, NULL)), 2) AS RH_Out
             ,ROUND(MAX(IF(sensor_id=4, sensor_value, NULL)), 2) AS NH3
             ,ROUND(MAX(IF(sensor_id=1, sensor_value, NULL)), 2) AS Temp
             ,ROUND(MAX(IF(sensor_id=2, sensor_value, NULL)), 2) AS RH
             ,ROUND(MAX(IF(sensor_id=7, sensor_value, NULL)), 2) AS Temp_P
             ,ROUND(MAX(IF(sensor_id=8, sensor_value, NULL)), 2) AS PH_P
             ,ROUND(MAX(IF(sensor_id=203, sensor_value, NULL)), 2) AS CH4
       FROM sensor_info_202410
      WHERE DATE_FORMAT(timestamp, '%Y%m%d') BETWEEN '20240901' AND '20241014'
      GROUP BY timestamp
      ) S
      LEFT JOIN AVERAGE_WEIGHT W ON S.create_dt = W.timestamp
      LEFT JOIN FEED_WEIGHT F ON S.create_dt = F.timestamp
ORDER BY S.create_dt



--
-- if 축사선택(select house) is selected with 자돈사 (pig house)
--
SELECT S.create_dt
       ,ROUND(W.AVERAGE_WEIGHT,2) AS AVERAGE_WEIGHT
       ,ROUND(F.FEED_WEIGHT,2) AS FEED_WEIGHT
       ,S.Temp_Out
       ,S.EC_P
       ,S.CO2
       ,ROUND(((SQRT(2*NVL(VR1,0)/1.3)*0.096) + (SQRT(2*NVL(VR2,0)/1.3)*0.126))/76.43097*60, 2) AS "Ventilation rate"
       ,S.RH_Out
       ,S.NH3
       ,S.Temp
       ,S.RH
       ,S.Temp_P
       ,S.PH_P
       ,S.CH4
  FROM (
      SELECT timestamp AS create_dt
             ,ROUND(NVL(MAX(IF(sensor_id=63, sensor_value, NULL)),0), 2) AS Temp_Out
             ,ROUND(MAX(IF(sensor_id=23, sensor_value, NULL)), 2) AS EC_P
             ,ROUND(MAX(IF(sensor_id=17, sensor_value, NULL)), 2) AS CO2
             ,ROUND(MAX(IF(sensor_id=27, sensor_value, NULL)), 2) AS VR1
             ,ROUND(MAX(IF(sensor_id=28, sensor_value, NULL)), 2) AS VR2
             ,ROUND(MAX(IF(sensor_id=64, sensor_value, NULL)), 2) AS RH_Out
             ,ROUND(MAX(IF(sensor_id=18, sensor_value, NULL)), 2) AS NH3
             ,ROUND(MAX(IF(sensor_id=15, sensor_value, NULL)), 2) AS Temp
             ,ROUND(MAX(IF(sensor_id=16, sensor_value, NULL)), 2) AS RH
             ,ROUND(MAX(IF(sensor_id=21, sensor_value, NULL)), 2) AS Temp_P
             ,ROUND(MAX(IF(sensor_id=22, sensor_value, NULL)), 2) AS PH_P
             ,ROUND(MAX(IF(sensor_id=203, sensor_value, NULL)), 2) AS CH4
       FROM sensor_info_202409
      WHERE DATE_FORMAT(timestamp, '%Y%m%d') BETWEEN '20240901' AND '20241014'
      GROUP BY timestamp
      UNION ALL
      SELECT timestamp AS create_dt
             ,ROUND(NVL(MAX(IF(sensor_id=63, sensor_value, NULL)),0), 2) AS Temp_Out
             ,ROUND(MAX(IF(sensor_id=23, sensor_value, NULL)), 2) AS EC_P
             ,ROUND(MAX(IF(sensor_id=17, sensor_value, NULL)), 2) AS CO2
             ,ROUND(MAX(IF(sensor_id=27, sensor_value, NULL)), 2) AS VR1
             ,ROUND(MAX(IF(sensor_id=28, sensor_value, NULL)), 2) AS VR2
             ,ROUND(MAX(IF(sensor_id=64, sensor_value, NULL)), 2) AS RH_Out
             ,ROUND(MAX(IF(sensor_id=18, sensor_value, NULL)), 2) AS NH3
             ,ROUND(MAX(IF(sensor_id=15, sensor_value, NULL)), 2) AS Temp
             ,ROUND(MAX(IF(sensor_id=16, sensor_value, NULL)), 2) AS RH
             ,ROUND(MAX(IF(sensor_id=21, sensor_value, NULL)), 2) AS Temp_P
             ,ROUND(MAX(IF(sensor_id=22, sensor_value, NULL)), 2) AS PH_P
             ,ROUND(MAX(IF(sensor_id=203, sensor_value, NULL)), 2) AS CH4
       FROM sensor_info_202410
      WHERE DATE_FORMAT(timestamp, '%Y%m%d') BETWEEN '20240901' AND '20241014'
      GROUP BY timestamp
      ) S
      LEFT JOIN AVERAGE_WEIGHT W ON S.create_dt = W.timestamp
      LEFT JOIN FEED_WEIGHT F ON S.create_dt = F.timestamp
ORDER BY S.create_dt
