How to create function in mysql many examples
DELIMITER $$
CREATE DEFINER=`skyaws`@`%` FUNCTION `calRh1730ForDS`(_data_date DATE,_stationId INTEGER) RETURNS double
BEGIN
DECLARE RES DOUBLE;
SELECT ROUND(Hmdt_Avg,1) INTO RES FROM STATION_DATA
WHERE DATE_FORMAT(DATA_DATE,'%Y-%m-%d %H:%i') = concat(_data_date,'17:30')
AND STATION_ID=_stationId;
RETURN RES;
END$$
DELIMITER ;
DELIMITER $$
CREATE DEFINER=`skyaws`@`%` FUNCTION `getMaxHumidityForCertificate`(dataDate TIMESTAMP,st_id INTEGER, formulaID INTEGER,cid INTEGER) RETURNS double
BEGIN
DECLARE RES DOUBLE;
IF (formulaID = 2) THEN
SELECT ROUND(MAX(Humidity),2) INTO RES FROM STATION_DATA st,COMPANY_STATION_MAPPING csm WHERE
st.IS_UPDATED=1 and
st.STATION_ID = st_id
AND DATA_DATE>=DATE_FORMAT(dataDate, '%Y-%m-%d 01:00:00') AND DATA_DATE<=DATE_FORMAT(DATE_ADD(dataDate, INTERVAL 1 DAY), '%Y-%m-%d 00:00:00')
AND DATE(DATA_DATE)<=DATE(DATA_TO_DATE) AND COMPANY_ID=cid AND csm.STATION_ID=st.STATION_ID;
ELSE
SELECT ROUND(MAX(Humidity),2) INTO RES FROM STATION_DATA WHERE
IS_UPDATED=1 and
STATION_ID = st_id
AND DATA_DATE>=DATE_FORMAT(dataDate, '%Y-%m-%d 00:00:00') AND DATA_DATE<=DATE_FORMAT(dataDate, '%Y-%m-%d 23:59:59');
END IF;
RETURN RES;
END$$
DELIMITER ;
DELIMITER $$
CREATE DEFINER=`skyaws`@`%` FUNCTION `getDirectionByAngle`(angle FLOAT) RETURNS text CHARSET utf8
BEGIN
DECLARE winddirection TEXT;
SELECT
CASE
WHEN 348.75<angle && angle<360 THEN 'N'
WHEN 0<=angle && angle<=11.25 THEN 'N'
WHEN 11.25<angle && angle<=33.75 THEN 'NNE'
WHEN 33.75<angle && angle<=56.25 THEN 'NE'
WHEN 56.25<angle && angle<=78.75 THEN 'ENE'
WHEN 78.75<angle && angle<=101.25 THEN 'E'
WHEN 101.25<angle && angle<=123.75 THEN 'ESE'
WHEN 123.75<angle && angle<=146.25 THEN 'SE'
WHEN 146.25<angle && angle<=168.75 THEN 'SSE'
WHEN 168.75<angle && angle<=191.25 THEN 'S'
WHEN 191.25<angle && angle<=213.75 THEN 'SSW'
WHEN 213.75<angle && angle<=236.25 THEN 'SW'
WHEN 236.25<angle && angle<=258.75 THEN 'WSW'
WHEN 258.75<angle && angle<=281.25 THEN 'W'
WHEN 281.25<angle && angle<=303.75 THEN 'WNW'
WHEN 303.75<angle && angle<=326.25 THEN 'NW'
WHEN 326.25<angle && angle<=348.75 THEN 'NNW'
ELSE 'N'
END AS dir INTO winddirection;
RETURN winddirection;
END$$
DELIMITER ;
DELIMITER $$
CREATE DEFINER=`root`@`localhost` FUNCTION `getDistanceFromLatlon`(lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT) RETURNS float
BEGIN SET @radi=6371; SET @dlat=RADIANS(lat1-lat2); SET @dlon=RADIANS(lon1-lon2); SET @a=( SIN(@dlat/2) * SIN(@dlat/2) + RADIANS(COS(lat1)) * RADIANS(COS(lat2)) * SIN(@dlon/2) * SIN(@dlon/2) ); SET @c=2*ATAN2(SQRT(@a), SQRT(1-@a)); SET @distance=@radi * @c; RETURN @distance;
END$$
DELIMITER ;
DELIMITER $$
CREATE DEFINER=`skyaws`@`%` FUNCTION `getMaxTempForCertificate`(dataDate TIMESTAMP,st_id INTEGER, formulaID INTEGER,cid INTEGER) RETURNS double
BEGIN
DECLARE RES DOUBLE;
IF (formulaID = 2) THEN
SELECT ROUND(MAX(PYRANO),2) INTO RES FROM STATION_DATA st,COMPANY_STATION_MAPPING csm WHERE
st.IS_UPDATED=1 and
st.STATION_ID = st_id
AND DATA_DATE>=DATE_FORMAT(dataDate, '%Y-%m-%d 01:00:00') AND DATA_DATE<=DATE_FORMAT(DATE_ADD(dataDate, INTERVAL 1 DAY), '%Y-%m-%d 00:00:00')
#AND DATE(DATA_DATE)<=DATE(DATA_TO_DATE)
AND COMPANY_ID=cid AND csm.STATION_ID=st.STATION_ID;
ELSE
SELECT ROUND(MAX(PYRANO),2) INTO RES FROM STATION_DATA WHERE
IS_UPDATED=1 and
STATION_ID = st_id
AND DATA_DATE>=DATE_FORMAT(dataDate, '%Y-%m-%d 00:00:00') AND DATA_DATE<=DATE_FORMAT(dataDate, '%Y-%m-%d 23:59:59');
END IF;
RETURN RES;
END$$
DELIMITER ;
No comments:
Post a Comment