目录
目标
空气质量指数计算对照表
计算IAQI函数
计算AQI函数
根据空气质量指数标准,通过调用MySQL函数来计算:二氧化硫(SO2)、二氧化氮(NO2)、一氧化碳(CO)、臭氧(O3)、臭氧8小时滑动(O38)、PM2.5、PM10的24小时平均浓度对应的IAQI和AQI。
DELIMITER $$
USE `epdp_lyg`$$
DROP FUNCTION IF EXISTS `IAQI_24H`$$
CREATE DEFINER=`root`@`%` FUNCTION `IAQI_24H`(param_factor_type VARCHAR(10),param_factor_value DECIMAL(10,5)) RETURNS VARCHAR(32) CHARSET utf8mb4
BEGIN
IF
param_factor_type IS NULL OR LENGTH(REPLACE(param_factor_type,' ',''))=0
OR param_factor_value IS NULL OR param_factor_value IS NULL
THEN
RETURN NULL;
END IF;
#二氧化硫(SO2)
IF
param_factor_type ="二氧化硫" OR LOWER(param_factor_type) ="so2"
THEN
IF
param_factor_value BETWEEN 0 AND 50
THEN
RETURN CEILING((50-0)/(50-0)*(param_factor_value-0)+0);
ELSEIF
param_factor_value BETWEEN 50 AND 150
THEN
RETURN CEILING((100-50)/(150-50)*(param_factor_value-50)+50);
ELSEIF
param_factor_value BETWEEN 150 AND 475
THEN
RETURN CEILING((150-100)/(475-150)*(param_factor_value-150)+100);
ELSEIF
param_factor_value BETWEEN 475 AND 800
THEN
RETURN CEILING((200-150)/(800-475)*(param_factor_value-475)+150);
ELSEIF
param_factor_value BETWEEN 800 AND 1600
THEN
RETURN CEILING((300-200)/(1600-800)*(param_factor_value-800)+200);
ELSEIF
param_factor_value BETWEEN 1600 AND 2100
THEN
RETURN CEILING((400-300)/(2100-1600)*(param_factor_value-1600)+300);
ELSEIF
param_factor_value BETWEEN 2100 AND 2620
THEN
RETURN CEILING((500-400)/(2620-2100)*(param_factor_value-2100)+400);
ELSE
RETURN NULL;
END IF;
END IF;
#二氧化氮(NO2)
IF
param_factor_type ="二氧化氮" OR LOWER(param_factor_type) ="no2"
THEN
IF
param_factor_value BETWEEN 0 AND 40
THEN
RETURN CEILING((50-0)/(40-0)*(param_factor_value-0)+0);
ELSEIF
param_factor_value BETWEEN 40 AND 80
THEN
RETURN CEILING((100-50)/(80-40)*(param_factor_value-40)+50);
ELSEIF
param_factor_value BETWEEN 80 AND 180
THEN
RETURN CEILING((150-100)/(180-80)*(param_factor_value-80)+100);
ELSEIF
param_factor_value BETWEEN 180 AND 280
THEN
RETURN CEILING((200-150)/(280-180)*(param_factor_value-180)+150);
ELSEIF
param_factor_value BETWEEN 280 AND 565
THEN
RETURN CEILING((300-200)/(565-280)*(param_factor_value-280)+200);
ELSEIF
param_factor_value BETWEEN 565 AND 750
THEN
RETURN CEILING((400-300)/(750-565)*(param_factor_value-565)+300);
ELSEIF
param_factor_value BETWEEN 750 AND 940
THEN
RETURN CEILING((500-400)/(940-750)*(param_factor_value-750)+400);
ELSE
RETURN NULL;
END IF;
END IF;
#一氧化碳(CO)
IF
param_factor_type ="一氧化碳" OR LOWER(param_factor_type) ="co"
THEN
IF
param_factor_value BETWEEN 0 AND 2
THEN
RETURN CEILING((50-0)/(2-0)*(param_factor_value-0)+0);
ELSEIF
param_factor_value BETWEEN 2 AND 4
THEN
RETURN CEILING((100-50)/(4-2)*(param_factor_value-2)+50);
ELSEIF
param_factor_value BETWEEN 4 AND 14
THEN
RETURN CEILING((150-100)/(14-4)*(param_factor_value-4)+100);
ELSEIF
param_factor_value BETWEEN 14 AND 24
THEN
RETURN CEILING((200-150)/(24-14)*(param_factor_value-14)+150);
ELSEIF
param_factor_value BETWEEN 24 AND 36
THEN
RETURN CEILING((300-200)/(36-24)*(param_factor_value-24)+200);
ELSEIF
param_factor_value BETWEEN 36 AND 48
THEN
RETURN CEILING((400-300)/(48-36)*(param_factor_value-36)+300);
ELSEIF
param_factor_value BETWEEN 48 AND 60
THEN
RETURN CEILING((500-400)/(60-48)*(param_factor_value-48)+400);
ELSE
RETURN NULL;
END IF;
END IF;
#臭氧8小时滑动(O38)& 臭氧(O3)
IF
LOWER(param_factor_type) ="o38" OR LOWER(param_factor_type) ="o3"
THEN
IF
param_factor_value BETWEEN 0 AND 100
THEN
RETURN CEILING((50-0)/(100-0)*(param_factor_value-0)+0);
ELSEIF
param_factor_value BETWEEN 100 AND 160
THEN
RETURN CEILING((100-50)/(160-100)*(param_factor_value-100)+50);
ELSEIF
param_factor_value BETWEEN 160 AND 215
THEN
RETURN CEILING((150-100)/(215-160)*(param_factor_value-160)+100);
ELSEIF
param_factor_value BETWEEN 215 AND 265
THEN
RETURN CEILING((200-150)/(265-215)*(param_factor_value-215)+150);
ELSEIF
param_factor_value BETWEEN 265 AND 800
THEN
RETURN CEILING((300-200)/(800-265)*(param_factor_value-265)+200);
ELSE
#臭氧高于800,8小时滑动IAQI按照1小时平均浓度计算的分指数报告。
RETURN NULL;
END IF;
END IF;
#PM2.5
IF
LOWER(param_factor_type) ="pm25" OR LOWER(param_factor_type) ="pm2.5"
THEN
IF
param_factor_value BETWEEN 0 AND 35
THEN
RETURN CEILING((50-0)/(35-0)*(param_factor_value-0)+0);
ELSEIF
param_factor_value BETWEEN 35 AND 75
THEN
RETURN CEILING((100-50)/(75-35)*(param_factor_value-35)+50);
ELSEIF
param_factor_value BETWEEN 75 AND 115
THEN
RETURN CEILING((150-100)/(115-75)*(param_factor_value-75)+100);
ELSEIF
param_factor_value BETWEEN 115 AND 150
THEN
RETURN CEILING((200-150)/(150-115)*(param_factor_value-115)+150);
ELSEIF
param_factor_value BETWEEN 150 AND 250
THEN
RETURN CEILING((300-200)/(250-150)*(param_factor_value-150)+200);
ELSEIF
param_factor_value BETWEEN 250 AND 350
THEN
RETURN CEILING((400-300)/(350-250)*(param_factor_value-250)+300);
ELSEIF
param_factor_value BETWEEN 350 AND 500
THEN
RETURN CEILING((500-400)/(500-350)*(param_factor_value-350)+400);
ELSE
RETURN NULL;
END IF;
END IF;
#PM10
IF
LOWER(param_factor_type) ="pm10"
THEN
IF
param_factor_value BETWEEN 0 AND 50
THEN
RETURN CEILING((50-0)/(50-0)*(param_factor_value-0)+0);
ELSEIF
param_factor_value BETWEEN 50 AND 150
THEN
RETURN CEILING((100-50)/(150-50)*(param_factor_value-50)+50);
ELSEIF
param_factor_value BETWEEN 150 AND 250
THEN
RETURN CEILING((150-100)/(250-150)*(param_factor_value-150)+100);
ELSEIF
param_factor_value BETWEEN 250 AND 350
THEN
RETURN CEILING((200-150)/(350-250)*(param_factor_value-250)+150);
ELSEIF
param_factor_value BETWEEN 350 AND 420
THEN
RETURN CEILING((300-200)/(420-350)*(param_factor_value-350)+200);
ELSEIF
param_factor_value BETWEEN 420 AND 500
THEN
RETURN CEILING((400-300)/(500-420)*(param_factor_value-420)+300);
ELSEIF
param_factor_value BETWEEN 500 AND 600
THEN
RETURN CEILING((500-400)/(600-500)*(param_factor_value-500)+400);
ELSE
RETURN NULL;
END IF;
END IF;
END$$
DELIMITER ;
调用测试
SELECT
`IAQI_24H`("so2",4.00) so2iaqi,
`IAQI_24H`("no2",30.00) no2iaqi,
`IAQI_24H`("co",0.700) coiaqi,
`IAQI_24H`("o38",119.00) o38iaqi,
`IAQI_24H`("pm25",21.00) pm25iaqi,
`IAQI_24H`("pm10",38.00) pm10iaqi
DELIMITER $$
USE `数据库名称`$$
DROP FUNCTION IF EXISTS `AQI_24H`$$
CREATE DEFINER=`数据库登录账号`@`%` FUNCTION `AQI_24H`(
param_so2_value DECIMAL(10,5),
param_no2_value DECIMAL(10,5),
param_co_value DECIMAL(10,5),
param_o38_value DECIMAL(10,5),
param_pm25_value DECIMAL(10,5),
param_pm10_value DECIMAL(10,5)
) RETURNS VARCHAR(32) CHARSET utf8mb4
BEGIN
DECLARE so2_iaqi DECIMAL(10,5);
DECLARE no2_iaqi DECIMAL(10,5);
DECLARE co_iaqi DECIMAL(10,5);
DECLARE o38_iaqi DECIMAL(10,5);
DECLARE pm25_iaqi DECIMAL(10,5);
DECLARE pm10_iaqi DECIMAL(10,5);
DECLARE aqi INT(5);
#二氧化硫(SO2)
IF
param_so2_value BETWEEN 0 AND 50
THEN
SET so2_iaqi = CEILING((50-0)/(50-0)*(param_so2_value-0)+0) ;
ELSEIF
param_so2_value BETWEEN 50 AND 150
THEN
SET so2_iaqi = CEILING((100-50)/(150-50)*(param_so2_value-50)+50);
ELSEIF
param_so2_value BETWEEN 150 AND 475
THEN
SET so2_iaqi = CEILING((150-100)/(475-150)*(param_so2_value-150)+100);
ELSEIF
param_so2_value BETWEEN 475 AND 800
THEN
SET so2_iaqi = CEILING((200-150)/(800-475)*(param_so2_value-475)+150);
ELSEIF
param_so2_value BETWEEN 800 AND 1600
THEN
SET so2_iaqi = CEILING((300-200)/(1600-800)*(param_so2_value-800)+200);
ELSEIF
param_so2_value BETWEEN 1600 AND 2100
THEN
SET so2_iaqi = CEILING((400-300)/(2100-1600)*(param_so2_value-1600)+300);
ELSEIF
param_so2_value BETWEEN 2100 AND 2620
THEN
SET so2_iaqi = CEILING((500-400)/(2620-2100)*(param_so2_value-2100)+400);
ELSE
SET so2_iaqi = NULL;
END IF;
#二氧化氮(NO2)
IF
param_no2_value BETWEEN 0 AND 40
THEN
SET no2_iaqi = CEILING((50-0)/(40-0)*(param_no2_value-0)+0);
ELSEIF
param_no2_value BETWEEN 40 AND 80
THEN
SET no2_iaqi = CEILING((100-50)/(80-40)*(param_no2_value-40)+50);
ELSEIF
param_no2_value BETWEEN 80 AND 180
THEN
SET no2_iaqi = CEILING((150-100)/(180-80)*(param_no2_value-80)+100);
ELSEIF
param_no2_value BETWEEN 180 AND 280
THEN
SET no2_iaqi = CEILING((200-150)/(280-180)*(param_no2_value-180)+150);
ELSEIF
param_no2_value BETWEEN 280 AND 565
THEN
SET no2_iaqi = CEILING((300-200)/(565-280)*(param_no2_value-280)+200);
ELSEIF
param_no2_value BETWEEN 565 AND 750
THEN
SET no2_iaqi = CEILING((400-300)/(750-565)*(param_no2_value-565)+300);
ELSEIF
param_no2_value BETWEEN 750 AND 940
THEN
SET no2_iaqi = CEILING((500-400)/(940-750)*(param_no2_value-750)+400);
ELSE
SET no2_iaqi = NULL;
END IF;
#一氧化碳(CO)
IF
param_co_value BETWEEN 0 AND 2
THEN
SET co_iaqi = CEILING((50-0)/(2-0)*(param_co_value-0)+0);
ELSEIF
param_co_value BETWEEN 2 AND 4
THEN
SET co_iaqi = CEILING((100-50)/(4-2)*(param_co_value-2)+50);
ELSEIF
param_co_value BETWEEN 4 AND 14
THEN
SET co_iaqi = CEILING((150-100)/(14-4)*(param_co_value-4)+100);
ELSEIF
param_co_value BETWEEN 14 AND 24
THEN
SET co_iaqi = CEILING((200-150)/(24-14)*(param_co_value-14)+150);
ELSEIF
param_co_value BETWEEN 24 AND 36
THEN
SET co_iaqi = CEILING((300-200)/(36-24)*(param_co_value-24)+200);
ELSEIF
param_co_value BETWEEN 36 AND 48
THEN
SET co_iaqi = CEILING((400-300)/(48-36)*(param_co_value-36)+300);
ELSEIF
param_co_value BETWEEN 48 AND 60
THEN
SET co_iaqi = CEILING((500-400)/(60-48)*(param_co_value-48)+400);
ELSE
SET co_iaqi = NULL;
END IF;
#臭氧8小时滑动(O38)
IF
param_o38_value BETWEEN 0 AND 100
THEN
SET o38_iaqi = CEILING((50-0)/(100-0)*(param_o38_value-0)+0);
ELSEIF
param_o38_value BETWEEN 100 AND 160
THEN
SET o38_iaqi = CEILING((100-50)/(160-100)*(param_o38_value-100)+50);
ELSEIF
param_o38_value BETWEEN 160 AND 215
THEN
SET o38_iaqi = CEILING((150-100)/(215-160)*(param_o38_value-160)+100);
ELSEIF
param_o38_value BETWEEN 215 AND 265
THEN
SET o38_iaqi = CEILING((200-150)/(265-215)*(param_o38_value-215)+150);
ELSEIF
param_o38_value BETWEEN 265 AND 800
THEN
SET o38_iaqi = CEILING((300-200)/(800-265)*(param_o38_value-265)+200);
ELSE
#臭氧高于800,8小时滑动IAQI按照1小时平均浓度计算的分指数报告。
SET o38_iaqi = NULL;
END IF;
#PM2.5
IF
param_pm25_value BETWEEN 0 AND 35
THEN
SET pm25_iaqi = CEILING((50-0)/(35-0)*(param_pm25_value-0)+0);
ELSEIF
param_pm25_value BETWEEN 35 AND 75
THEN
SET pm25_iaqi = CEILING((100-50)/(75-35)*(param_pm25_value-35)+50);
ELSEIF
param_pm25_value BETWEEN 75 AND 115
THEN
SET pm25_iaqi = CEILING((150-100)/(115-75)*(param_pm25_value-75)+100);
ELSEIF
param_pm25_value BETWEEN 115 AND 150
THEN
SET pm25_iaqi = CEILING((200-150)/(150-115)*(param_pm25_value-115)+150);
ELSEIF
param_pm25_value BETWEEN 150 AND 250
THEN
SET pm25_iaqi = CEILING((300-200)/(250-150)*(param_pm25_value-150)+200);
ELSEIF
param_pm25_value BETWEEN 250 AND 350
THEN
SET pm25_iaqi = CEILING((400-300)/(350-250)*(param_pm25_value-250)+300);
ELSEIF
param_pm25_value BETWEEN 350 AND 500
THEN
SET pm25_iaqi = CEILING((500-400)/(500-350)*(param_pm25_value-350)+400);
ELSE
SET pm25_iaqi = NULL;
END IF;
#PM10
IF
param_pm10_value BETWEEN 0 AND 50
THEN
SET pm10_iaqi = CEILING((50-0)/(50-0)*(param_pm10_value-0)+0);
ELSEIF
param_pm10_value BETWEEN 50 AND 150
THEN
SET pm10_iaqi = CEILING((100-50)/(150-50)*(param_pm10_value-50)+50);
ELSEIF
param_pm10_value BETWEEN 150 AND 250
THEN
SET pm10_iaqi = CEILING((150-100)/(250-150)*(param_pm10_value-150)+100);
ELSEIF
param_pm10_value BETWEEN 250 AND 350
THEN
SET pm10_iaqi = CEILING((200-150)/(350-250)*(param_pm10_value-250)+150);
ELSEIF
param_pm10_value BETWEEN 350 AND 420
THEN
SET pm10_iaqi = CEILING((300-200)/(420-350)*(param_pm10_value-350)+200);
ELSEIF
param_pm10_value BETWEEN 420 AND 500
THEN
SET pm10_iaqi = CEILING((400-300)/(500-420)*(param_pm10_value-420)+300);
ELSEIF
param_pm10_value BETWEEN 500 AND 600
THEN
SET pm10_iaqi = CEILING((500-400)/(600-500)*(param_pm10_value-500)+400);
ELSE
SET pm10_iaqi = NULL;
END IF;
SELECT MAX(iaqi) INTO aqi FROM(
SELECT so2_iaqi iaqi
UNION ALL
SELECT no2_iaqi
UNION ALL
SELECT co_iaqi
UNION ALL
SELECT o38_iaqi
UNION ALL
SELECT pm25_iaqi
UNION ALL
SELECT pm10_iaqi
)a;
RETURN aqi;
END$$
DELIMITER ;
调用测试
#参数顺序:so2,no2,co,o38,pm25,pm10
SELECT AQI_24H(4.00,30.00,0.700,119.00,21.00,38.00) aqi