테이블이 y1 과 y2 가 있고
y1 의 계정이 'A201130+A201131-(M201131+YqM(M111900+(M216890/M211350)-M112400)+A298300-M112444 처럼 수식으로 이루어 졌을때 YoY 를 구하는
mysql function 이다.
YoY 는 전년 대비 현재 년도 증가분이다.
((당해년도데이터 - 전년도데이터 )/전년도데이터 ) * 100 이다.
DROP TABLE IF EXISTS EXAMPLE;
CREATE TABLE `EXAMPLE` ( CNT INT , RETURN_STR VARCHAR(2000), V_OPERATOR VARCHAR(20), LEFT_STR VARCHAR(2000));
delimiter $$
DROP FUNCTION IF EXISTS YoY$$
CREATE FUNCTION YoY( strFormular VARCHAR(20000),T1 varchar(50),T2 varchar(50) ) RETURNS VARCHAR(20000)
BEGIN
DECLARE V_RETURN_STR VARCHAR(20000);
DECLARE V_RETURN_STR_LEFT VARCHAR(20000);
DECLARE V_RETURN_STR_INSIDE VARCHAR(20000);
DECLARE V_RETURN_STR_RIGHT VARCHAR(20000);
DECLARE V_STR_LEFT VARCHAR(20000);
DECLARE V_STR_RIGHT VARCHAR(20000);
DECLARE V_STR_INSIDE VARCHAR(20000);
DECLARE V_STR_INSIDE_T1 VARCHAR(20000);
DECLARE V_STR_INSIDE_T2 VARCHAR(20000);
DECLARE V_VALUE VARCHAR(20000);
DECLARE V_FORMULA VARCHAR(20000);
DECLARE V_CNT INT;
DECLARE V_MIN_INDEX INT;
DECLARE V_OPERATOR VARCHAR(1);
DECLARE V_IS_END int;
DECLARE V_INDEX int;
SET V_RETURN_STR_LEFT='';
SET V_RETURN_STR_INSIDE='';
SET V_RETURN_STR_RIGHT='';
SET V_VALUE = '';
SET V_STR_LEFT = SUBSTRING(strFormular,1,INSTR(strFormular,'YqM')-1);
SET V_STR_RIGHT = SUBSTRING(strFormular,INSTR(strFormular,'YqM')+3,length(strFormular)-INSTR(strFormular,'YqM')+3) ;
SET V_STR_INSIDE = '';
SET V_CNT = 0;
-- STEP 0. YOY QOQ YQM 여부 판별
SET V_FORMULA = IF(INSTR(strFormular,'YoY') > 0,'YoY',IF(INSTR(strFormular,'QoQ') > 0,'QoQ','YqM'));
-- STEP 1. 함수 왼쪽처리
WHILE (IF(LOCATE('+', V_STR_LEFT)
|| LOCATE('-', V_STR_LEFT)>0
|| LOCATE('X', V_STR_LEFT)>0
|| LOCATE('/', V_STR_LEFT)>0
|| LOCATE(')', V_STR_LEFT)>0
|| LOCATE('(', V_STR_LEFT)>0
,1,0) > 0)
DO
SET V_MIN_INDEX = length(V_STR_LEFT);
SET V_OPERATOR = '';
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'+') > 0 AND INSTR(V_STR_LEFT,'+') < V_MIN_INDEX, INSTR(V_STR_LEFT,'+'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'+') = V_MIN_INDEX, '+',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'-') > 0 AND INSTR(V_STR_LEFT,'-') < V_MIN_INDEX, INSTR(V_STR_LEFT,'-'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'-') = V_MIN_INDEX, '-',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'*') > 0 AND INSTR(V_STR_LEFT,'*') < V_MIN_INDEX, INSTR(V_STR_LEFT,'*'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'*') = V_MIN_INDEX, '*',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'/') > 0 AND INSTR(V_STR_LEFT,'/') < V_MIN_INDEX, INSTR(V_STR_LEFT,'/'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'/') = V_MIN_INDEX, '/',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'(') > 0 AND INSTR(V_STR_LEFT,'(') < V_MIN_INDEX, INSTR(V_STR_LEFT,'('),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'(') = V_MIN_INDEX, '(',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,')') > 0 AND INSTR(V_STR_LEFT,')') < V_MIN_INDEX, INSTR(V_STR_LEFT,')'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,')') = V_MIN_INDEX, ')',V_OPERATOR);
SET V_VALUE = SUBSTRING(V_STR_LEFT,1,INSTR(V_STR_LEFT,V_OPERATOR)-1);
SET V_STR_LEFT= SUBSTRING(V_STR_LEFT, LOCATE(V_OPERATOR,V_STR_LEFT) + 1);
SET V_RETURN_STR_LEFT = CONCAT( V_RETURN_STR_LEFT ,IF(LENGTH(V_VALUE) = 0,V_VALUE,CONCAT(T1,'.',V_VALUE)),V_OPERATOR);
SET V_CNT = V_CNT + 1;
-- INSERT INTO EXAMPLE VALUES(V_CNT,V_RETURN_STR,V_OPERATOR,V_STR_RIGHT);
END WHILE;
-- INSERT INTO EXAMPLE VALUES(1,V_RETURN_STR_LEFT,'','왼쪽');
-- setp 2.1 yQm 열린 괄호 갯수 세기
SET V_CNT = 0;
-- SET V_STR_LEFT = SUBSTRING(strFormular,INSTR(strFormular,'YqM')+3,length(strFormular)-INSTR(strFormular,'YqM')+3) ;
SET V_STR_LEFT = SUBSTRING(strFormular,INSTR(strFormular,V_FORMULA)+3,length(strFormular)-INSTR(strFormular,V_FORMULA)+3) ;
SET V_IS_END = 0;
SET V_INDEX = 0;
WHILE ( V_IS_END = 0)
DO
SET V_INDEX = V_INDEX + 1;
SET V_CNT = V_CNT + IF(SUBSTRING(V_STR_LEFT,V_INDEX,1) = '(',1,IF(SUBSTRING(V_STR_LEFT,V_INDEX,1) = ')',-1,0));
SET V_IS_END = IF(V_CNT = 0, 1,0);
-- INSERT INTO EXAMPLE VALUES(V_INDEX,SUBSTRING(V_STR_LEFT,V_INDEX,1),'',V_STR_LEFT);
END WHILE;
SET V_STR_INSIDE = MID(V_STR_LEFT,1,V_INDEX);
SET V_STR_RIGHT = SUBSTRING(V_STR_LEFT,V_INDEX+1);
-- INSERT INTO EXAMPLE VALUES(V_INDEX,V_STR_INSIDE,'',V_STR_RIGHT);
-- setp 2.2 T1 만들기
SET V_STR_INSIDE_T1 = '';
SET V_STR_LEFT = V_STR_INSIDE;
WHILE (IF(LOCATE('+', V_STR_LEFT)
|| LOCATE('-', V_STR_LEFT)>0
|| LOCATE('X', V_STR_LEFT)>0
|| LOCATE('/', V_STR_LEFT)>0
|| LOCATE(')', V_STR_LEFT)>0
|| LOCATE('(', V_STR_LEFT)>0
,1,0) > 0)
DO
SET V_MIN_INDEX = length(V_STR_LEFT);
SET V_OPERATOR = '';
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'+') > 0 AND INSTR(V_STR_LEFT,'+') < V_MIN_INDEX, INSTR(V_STR_LEFT,'+'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'+') = V_MIN_INDEX, '+',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'-') > 0 AND INSTR(V_STR_LEFT,'-') < V_MIN_INDEX, INSTR(V_STR_LEFT,'-'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'-') = V_MIN_INDEX, '-',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'*') > 0 AND INSTR(V_STR_LEFT,'*') < V_MIN_INDEX, INSTR(V_STR_LEFT,'*'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'*') = V_MIN_INDEX, '*',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'/') > 0 AND INSTR(V_STR_LEFT,'/') < V_MIN_INDEX, INSTR(V_STR_LEFT,'/'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'/') = V_MIN_INDEX, '/',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'(') > 0 AND INSTR(V_STR_LEFT,'(') < V_MIN_INDEX, INSTR(V_STR_LEFT,'('),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'(') = V_MIN_INDEX, '(',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,')') > 0 AND INSTR(V_STR_LEFT,')') < V_MIN_INDEX, INSTR(V_STR_LEFT,')'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,')') = V_MIN_INDEX, ')',V_OPERATOR);
SET V_VALUE = SUBSTRING(V_STR_LEFT,1,INSTR(V_STR_LEFT,V_OPERATOR)-1);
SET V_STR_LEFT= SUBSTRING(V_STR_LEFT, LOCATE(V_OPERATOR,V_STR_LEFT) + 1);
-- SET V_STR_INSIDE_T1 = CONCAT( V_STR_INSIDE_T1 ,IF(V_OPERATOR = '(' OR V_OPERATOR = ')' ,V_VALUE,CONCAT(T1,'.',V_VALUE)),V_OPERATOR);
SET V_STR_INSIDE_T1 = CONCAT( V_STR_INSIDE_T1 ,IF(LENGTH(V_VALUE) = 0,V_VALUE,CONCAT(T1,'.',V_VALUE)),V_OPERATOR);
SET V_CNT = V_CNT + 1;
-- INSERT INTO EXAMPLE VALUES(V_CNT,V_STR_INSIDE_T1,V_OPERATOR,CONCAT('VALUE: ',V_VALUE,' : STR_LEFT:',V_STR_LEFT));
END WHILE;
-- INSERT INTO EXAMPLE VALUES(2,V_STR_INSIDE_T1,'','yqm t1');
-- setp 2.3 T2 만들기
SET V_STR_INSIDE_T2 = '';
SET V_STR_LEFT = V_STR_INSIDE;
WHILE (IF(LOCATE('+', V_STR_LEFT)
|| LOCATE('-', V_STR_LEFT)>0
|| LOCATE('X', V_STR_LEFT)>0
|| LOCATE('/', V_STR_LEFT)>0
|| LOCATE(')', V_STR_LEFT)>0
|| LOCATE('(', V_STR_LEFT)>0
,1,0) > 0)
DO
SET V_MIN_INDEX = length(V_STR_LEFT);
SET V_OPERATOR = '';
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'+') > 0 AND INSTR(V_STR_LEFT,'+') < V_MIN_INDEX, INSTR(V_STR_LEFT,'+'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'+') = V_MIN_INDEX, '+',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'-') > 0 AND INSTR(V_STR_LEFT,'-') < V_MIN_INDEX, INSTR(V_STR_LEFT,'-'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'-') = V_MIN_INDEX, '-',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'*') > 0 AND INSTR(V_STR_LEFT,'*') < V_MIN_INDEX, INSTR(V_STR_LEFT,'*'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'*') = V_MIN_INDEX, '*',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'/') > 0 AND INSTR(V_STR_LEFT,'/') < V_MIN_INDEX, INSTR(V_STR_LEFT,'/'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'/') = V_MIN_INDEX, '/',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'(') > 0 AND INSTR(V_STR_LEFT,'(') < V_MIN_INDEX, INSTR(V_STR_LEFT,'('),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'(') = V_MIN_INDEX, '(',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,')') > 0 AND INSTR(V_STR_LEFT,')') < V_MIN_INDEX, INSTR(V_STR_LEFT,')'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,')') = V_MIN_INDEX, ')',V_OPERATOR);
SET V_VALUE = SUBSTRING(V_STR_LEFT,1,INSTR(V_STR_LEFT,V_OPERATOR)-1);
SET V_STR_LEFT= SUBSTRING(V_STR_LEFT, LOCATE(V_OPERATOR,V_STR_LEFT) + 1);
-- SET V_STR_INSIDE_T1 = CONCAT( V_STR_INSIDE_T1 ,IF(V_OPERATOR = '(' OR V_OPERATOR = ')' ,V_VALUE,CONCAT(T1,'.',V_VALUE)),V_OPERATOR);
SET V_STR_INSIDE_T2 = CONCAT( V_STR_INSIDE_T2 ,IF(LENGTH(V_VALUE) = 0,V_VALUE,CONCAT(T2,'.',V_VALUE)),V_OPERATOR);
SET V_CNT = V_CNT + 1;
-- INSERT INTO EXAMPLE VALUES(V_CNT,V_STR_INSIDE_T2,V_OPERATOR,CONCAT('VALUE: ',V_VALUE,' : STR_LEFT:',V_STR_LEFT));
END WHILE;
-- INSERT INTO EXAMPLE VALUES(3,V_STR_INSIDE_T2,'','yqm t2');
-- STEP 3. 함수 오른쪽처리
SET V_STR_LEFT = V_STR_RIGHT;
WHILE (IF(LOCATE('+', V_STR_LEFT)
|| LOCATE('-', V_STR_LEFT)>0
|| LOCATE('X', V_STR_LEFT)>0
|| LOCATE('/', V_STR_LEFT)>0
|| LOCATE(')', V_STR_LEFT)>0
|| LOCATE('(', V_STR_LEFT)>0
,1,0) > 0)
DO
SET V_MIN_INDEX = length(V_STR_LEFT);
SET V_OPERATOR = '';
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'+') > 0 AND INSTR(V_STR_LEFT,'+') < V_MIN_INDEX, INSTR(V_STR_LEFT,'+'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'+') = V_MIN_INDEX, '+',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'-') > 0 AND INSTR(V_STR_LEFT,'-') < V_MIN_INDEX, INSTR(V_STR_LEFT,'-'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'-') = V_MIN_INDEX, '-',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'*') > 0 AND INSTR(V_STR_LEFT,'*') < V_MIN_INDEX, INSTR(V_STR_LEFT,'*'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'*') = V_MIN_INDEX, '*',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'/') > 0 AND INSTR(V_STR_LEFT,'/') < V_MIN_INDEX, INSTR(V_STR_LEFT,'/'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'/') = V_MIN_INDEX, '/',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'(') > 0 AND INSTR(V_STR_LEFT,'(') < V_MIN_INDEX, INSTR(V_STR_LEFT,'('),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'(') = V_MIN_INDEX, '(',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,')') > 0 AND INSTR(V_STR_LEFT,')') < V_MIN_INDEX, INSTR(V_STR_LEFT,')'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,')') = V_MIN_INDEX, ')',V_OPERATOR);
SET V_VALUE = SUBSTRING(V_STR_LEFT,1,INSTR(V_STR_LEFT,V_OPERATOR)-1);
SET V_STR_LEFT= SUBSTRING(V_STR_LEFT, LOCATE(V_OPERATOR,V_STR_LEFT) + 1);
-- SET V_RETURN_STR_RIGHT = CONCAT( V_RETURN_STR_RIGHT ,IF(V_OPERATOR = '(' OR V_OPERATOR = ')' ,V_VALUE,CONCAT(T1,'.',V_VALUE)),V_OPERATOR);
SET V_RETURN_STR_RIGHT = CONCAT( V_RETURN_STR_RIGHT ,IF(LENGTH(V_VALUE) = 0,V_VALUE,CONCAT(T1,'.',V_VALUE)),V_OPERATOR);
SET V_CNT = V_CNT + 1;
-- INSERT INTO EXAMPLE VALUES(V_CNT,V_RETURN_STR,V_OPERATOR,V_STR_RIGHT);
END WHILE;
-- INSERT INTO EXAMPLE VALUES(4,V_RETURN_STR_RIGHT,'','오른쪽');
-- 공식의 종류에 따른 출력값을 결정한다. YqM 인지만 판별 하면됨
SET V_RETURN_STR_INSIDE = IF(V_FORMULA = 'YqM',CONCAT('(',V_STR_INSIDE_T2,'-',V_STR_INSIDE_T1,')')
, CONCAT('\t((\t' ,V_STR_INSIDE_T1 ,'-' ,V_STR_INSIDE_T1 ,')/' ,V_STR_INSIDE_T2 ,')*100\t'));
-- ,'-' ,V_STR_INSIDE_T1 ,')/' ,V_STR_INSIDE_T2 ,')*100' ); // YqM 아니면 다 YoY 로 해석
SET V_RETURN_STR = CONCAT(V_RETURN_STR_LEFT,V_RETURN_STR_INSIDE,V_RETURN_STR_RIGHT);
-- INSERT INTO EXAMPLE VALUES(5,V_RETURN_STR,'','결과');
RETURN V_RETURN_STR;
-- RETURN @value;
END $$
delimiter ;
SELECT YoY('A201130+A201131-(M201131+YqM(M111900+(M216890/M211350)-M112400)+A298300-M112444)','T_FIN_DATA_Y0','T_FIN_DATA_Y1') YoY;
-- SELECT * FROM EXAMPLE;
/*
SET @EMAIL = 'A201130+YqM(M111900+(M216890/M211350)-M112400)+A298300-M112444';
SELECT @EMAIL,SUBSTRING(@email,1,INSTR(@email,'YqM')-1) leftStr,SUBSTRING_INDEX(@email,'YqM',-1) rightStr*/
SELECT YoY('YoY(A201130)','T_FIN_DATA_Y0','T_FIN_DATA_Y1') YoY
union
SELECT YoY('YoY(A201130-M201000)','T_FIN_DATA_Y0','T_FIN_DATA_Y1') YoY
union
SELECT YoY('YqM(A201130)','T_FIN_DATA_Y0','T_FIN_DATA_Y1') YoY;
y1 의 계정이 'A201130+A201131-(M201131+YqM(M111900+(M216890/M211350)-M112400)+A298300-M112444 처럼 수식으로 이루어 졌을때 YoY 를 구하는
mysql function 이다.
YoY 는 전년 대비 현재 년도 증가분이다.
((당해년도데이터 - 전년도데이터 )/전년도데이터 ) * 100 이다.
DROP TABLE IF EXISTS EXAMPLE;
CREATE TABLE `EXAMPLE` ( CNT INT , RETURN_STR VARCHAR(2000), V_OPERATOR VARCHAR(20), LEFT_STR VARCHAR(2000));
delimiter $$
DROP FUNCTION IF EXISTS YoY$$
CREATE FUNCTION YoY( strFormular VARCHAR(20000),T1 varchar(50),T2 varchar(50) ) RETURNS VARCHAR(20000)
BEGIN
DECLARE V_RETURN_STR VARCHAR(20000);
DECLARE V_RETURN_STR_LEFT VARCHAR(20000);
DECLARE V_RETURN_STR_INSIDE VARCHAR(20000);
DECLARE V_RETURN_STR_RIGHT VARCHAR(20000);
DECLARE V_STR_LEFT VARCHAR(20000);
DECLARE V_STR_RIGHT VARCHAR(20000);
DECLARE V_STR_INSIDE VARCHAR(20000);
DECLARE V_STR_INSIDE_T1 VARCHAR(20000);
DECLARE V_STR_INSIDE_T2 VARCHAR(20000);
DECLARE V_VALUE VARCHAR(20000);
DECLARE V_FORMULA VARCHAR(20000);
DECLARE V_CNT INT;
DECLARE V_MIN_INDEX INT;
DECLARE V_OPERATOR VARCHAR(1);
DECLARE V_IS_END int;
DECLARE V_INDEX int;
SET V_RETURN_STR_LEFT='';
SET V_RETURN_STR_INSIDE='';
SET V_RETURN_STR_RIGHT='';
SET V_VALUE = '';
SET V_STR_LEFT = SUBSTRING(strFormular,1,INSTR(strFormular,'YqM')-1);
SET V_STR_RIGHT = SUBSTRING(strFormular,INSTR(strFormular,'YqM')+3,length(strFormular)-INSTR(strFormular,'YqM')+3) ;
SET V_STR_INSIDE = '';
SET V_CNT = 0;
-- STEP 0. YOY QOQ YQM 여부 판별
SET V_FORMULA = IF(INSTR(strFormular,'YoY') > 0,'YoY',IF(INSTR(strFormular,'QoQ') > 0,'QoQ','YqM'));
-- STEP 1. 함수 왼쪽처리
WHILE (IF(LOCATE('+', V_STR_LEFT)
|| LOCATE('-', V_STR_LEFT)>0
|| LOCATE('X', V_STR_LEFT)>0
|| LOCATE('/', V_STR_LEFT)>0
|| LOCATE(')', V_STR_LEFT)>0
|| LOCATE('(', V_STR_LEFT)>0
,1,0) > 0)
DO
SET V_MIN_INDEX = length(V_STR_LEFT);
SET V_OPERATOR = '';
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'+') > 0 AND INSTR(V_STR_LEFT,'+') < V_MIN_INDEX, INSTR(V_STR_LEFT,'+'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'+') = V_MIN_INDEX, '+',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'-') > 0 AND INSTR(V_STR_LEFT,'-') < V_MIN_INDEX, INSTR(V_STR_LEFT,'-'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'-') = V_MIN_INDEX, '-',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'*') > 0 AND INSTR(V_STR_LEFT,'*') < V_MIN_INDEX, INSTR(V_STR_LEFT,'*'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'*') = V_MIN_INDEX, '*',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'/') > 0 AND INSTR(V_STR_LEFT,'/') < V_MIN_INDEX, INSTR(V_STR_LEFT,'/'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'/') = V_MIN_INDEX, '/',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'(') > 0 AND INSTR(V_STR_LEFT,'(') < V_MIN_INDEX, INSTR(V_STR_LEFT,'('),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'(') = V_MIN_INDEX, '(',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,')') > 0 AND INSTR(V_STR_LEFT,')') < V_MIN_INDEX, INSTR(V_STR_LEFT,')'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,')') = V_MIN_INDEX, ')',V_OPERATOR);
SET V_VALUE = SUBSTRING(V_STR_LEFT,1,INSTR(V_STR_LEFT,V_OPERATOR)-1);
SET V_STR_LEFT= SUBSTRING(V_STR_LEFT, LOCATE(V_OPERATOR,V_STR_LEFT) + 1);
SET V_RETURN_STR_LEFT = CONCAT( V_RETURN_STR_LEFT ,IF(LENGTH(V_VALUE) = 0,V_VALUE,CONCAT(T1,'.',V_VALUE)),V_OPERATOR);
SET V_CNT = V_CNT + 1;
-- INSERT INTO EXAMPLE VALUES(V_CNT,V_RETURN_STR,V_OPERATOR,V_STR_RIGHT);
END WHILE;
-- INSERT INTO EXAMPLE VALUES(1,V_RETURN_STR_LEFT,'','왼쪽');
-- setp 2.1 yQm 열린 괄호 갯수 세기
SET V_CNT = 0;
-- SET V_STR_LEFT = SUBSTRING(strFormular,INSTR(strFormular,'YqM')+3,length(strFormular)-INSTR(strFormular,'YqM')+3) ;
SET V_STR_LEFT = SUBSTRING(strFormular,INSTR(strFormular,V_FORMULA)+3,length(strFormular)-INSTR(strFormular,V_FORMULA)+3) ;
SET V_IS_END = 0;
SET V_INDEX = 0;
WHILE ( V_IS_END = 0)
DO
SET V_INDEX = V_INDEX + 1;
SET V_CNT = V_CNT + IF(SUBSTRING(V_STR_LEFT,V_INDEX,1) = '(',1,IF(SUBSTRING(V_STR_LEFT,V_INDEX,1) = ')',-1,0));
SET V_IS_END = IF(V_CNT = 0, 1,0);
-- INSERT INTO EXAMPLE VALUES(V_INDEX,SUBSTRING(V_STR_LEFT,V_INDEX,1),'',V_STR_LEFT);
END WHILE;
SET V_STR_INSIDE = MID(V_STR_LEFT,1,V_INDEX);
SET V_STR_RIGHT = SUBSTRING(V_STR_LEFT,V_INDEX+1);
-- INSERT INTO EXAMPLE VALUES(V_INDEX,V_STR_INSIDE,'',V_STR_RIGHT);
-- setp 2.2 T1 만들기
SET V_STR_INSIDE_T1 = '';
SET V_STR_LEFT = V_STR_INSIDE;
WHILE (IF(LOCATE('+', V_STR_LEFT)
|| LOCATE('-', V_STR_LEFT)>0
|| LOCATE('X', V_STR_LEFT)>0
|| LOCATE('/', V_STR_LEFT)>0
|| LOCATE(')', V_STR_LEFT)>0
|| LOCATE('(', V_STR_LEFT)>0
,1,0) > 0)
DO
SET V_MIN_INDEX = length(V_STR_LEFT);
SET V_OPERATOR = '';
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'+') > 0 AND INSTR(V_STR_LEFT,'+') < V_MIN_INDEX, INSTR(V_STR_LEFT,'+'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'+') = V_MIN_INDEX, '+',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'-') > 0 AND INSTR(V_STR_LEFT,'-') < V_MIN_INDEX, INSTR(V_STR_LEFT,'-'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'-') = V_MIN_INDEX, '-',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'*') > 0 AND INSTR(V_STR_LEFT,'*') < V_MIN_INDEX, INSTR(V_STR_LEFT,'*'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'*') = V_MIN_INDEX, '*',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'/') > 0 AND INSTR(V_STR_LEFT,'/') < V_MIN_INDEX, INSTR(V_STR_LEFT,'/'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'/') = V_MIN_INDEX, '/',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'(') > 0 AND INSTR(V_STR_LEFT,'(') < V_MIN_INDEX, INSTR(V_STR_LEFT,'('),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'(') = V_MIN_INDEX, '(',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,')') > 0 AND INSTR(V_STR_LEFT,')') < V_MIN_INDEX, INSTR(V_STR_LEFT,')'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,')') = V_MIN_INDEX, ')',V_OPERATOR);
SET V_VALUE = SUBSTRING(V_STR_LEFT,1,INSTR(V_STR_LEFT,V_OPERATOR)-1);
SET V_STR_LEFT= SUBSTRING(V_STR_LEFT, LOCATE(V_OPERATOR,V_STR_LEFT) + 1);
-- SET V_STR_INSIDE_T1 = CONCAT( V_STR_INSIDE_T1 ,IF(V_OPERATOR = '(' OR V_OPERATOR = ')' ,V_VALUE,CONCAT(T1,'.',V_VALUE)),V_OPERATOR);
SET V_STR_INSIDE_T1 = CONCAT( V_STR_INSIDE_T1 ,IF(LENGTH(V_VALUE) = 0,V_VALUE,CONCAT(T1,'.',V_VALUE)),V_OPERATOR);
SET V_CNT = V_CNT + 1;
-- INSERT INTO EXAMPLE VALUES(V_CNT,V_STR_INSIDE_T1,V_OPERATOR,CONCAT('VALUE: ',V_VALUE,' : STR_LEFT:',V_STR_LEFT));
END WHILE;
-- INSERT INTO EXAMPLE VALUES(2,V_STR_INSIDE_T1,'','yqm t1');
-- setp 2.3 T2 만들기
SET V_STR_INSIDE_T2 = '';
SET V_STR_LEFT = V_STR_INSIDE;
WHILE (IF(LOCATE('+', V_STR_LEFT)
|| LOCATE('-', V_STR_LEFT)>0
|| LOCATE('X', V_STR_LEFT)>0
|| LOCATE('/', V_STR_LEFT)>0
|| LOCATE(')', V_STR_LEFT)>0
|| LOCATE('(', V_STR_LEFT)>0
,1,0) > 0)
DO
SET V_MIN_INDEX = length(V_STR_LEFT);
SET V_OPERATOR = '';
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'+') > 0 AND INSTR(V_STR_LEFT,'+') < V_MIN_INDEX, INSTR(V_STR_LEFT,'+'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'+') = V_MIN_INDEX, '+',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'-') > 0 AND INSTR(V_STR_LEFT,'-') < V_MIN_INDEX, INSTR(V_STR_LEFT,'-'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'-') = V_MIN_INDEX, '-',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'*') > 0 AND INSTR(V_STR_LEFT,'*') < V_MIN_INDEX, INSTR(V_STR_LEFT,'*'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'*') = V_MIN_INDEX, '*',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'/') > 0 AND INSTR(V_STR_LEFT,'/') < V_MIN_INDEX, INSTR(V_STR_LEFT,'/'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'/') = V_MIN_INDEX, '/',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'(') > 0 AND INSTR(V_STR_LEFT,'(') < V_MIN_INDEX, INSTR(V_STR_LEFT,'('),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'(') = V_MIN_INDEX, '(',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,')') > 0 AND INSTR(V_STR_LEFT,')') < V_MIN_INDEX, INSTR(V_STR_LEFT,')'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,')') = V_MIN_INDEX, ')',V_OPERATOR);
SET V_VALUE = SUBSTRING(V_STR_LEFT,1,INSTR(V_STR_LEFT,V_OPERATOR)-1);
SET V_STR_LEFT= SUBSTRING(V_STR_LEFT, LOCATE(V_OPERATOR,V_STR_LEFT) + 1);
-- SET V_STR_INSIDE_T1 = CONCAT( V_STR_INSIDE_T1 ,IF(V_OPERATOR = '(' OR V_OPERATOR = ')' ,V_VALUE,CONCAT(T1,'.',V_VALUE)),V_OPERATOR);
SET V_STR_INSIDE_T2 = CONCAT( V_STR_INSIDE_T2 ,IF(LENGTH(V_VALUE) = 0,V_VALUE,CONCAT(T2,'.',V_VALUE)),V_OPERATOR);
SET V_CNT = V_CNT + 1;
-- INSERT INTO EXAMPLE VALUES(V_CNT,V_STR_INSIDE_T2,V_OPERATOR,CONCAT('VALUE: ',V_VALUE,' : STR_LEFT:',V_STR_LEFT));
END WHILE;
-- INSERT INTO EXAMPLE VALUES(3,V_STR_INSIDE_T2,'','yqm t2');
-- STEP 3. 함수 오른쪽처리
SET V_STR_LEFT = V_STR_RIGHT;
WHILE (IF(LOCATE('+', V_STR_LEFT)
|| LOCATE('-', V_STR_LEFT)>0
|| LOCATE('X', V_STR_LEFT)>0
|| LOCATE('/', V_STR_LEFT)>0
|| LOCATE(')', V_STR_LEFT)>0
|| LOCATE('(', V_STR_LEFT)>0
,1,0) > 0)
DO
SET V_MIN_INDEX = length(V_STR_LEFT);
SET V_OPERATOR = '';
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'+') > 0 AND INSTR(V_STR_LEFT,'+') < V_MIN_INDEX, INSTR(V_STR_LEFT,'+'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'+') = V_MIN_INDEX, '+',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'-') > 0 AND INSTR(V_STR_LEFT,'-') < V_MIN_INDEX, INSTR(V_STR_LEFT,'-'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'-') = V_MIN_INDEX, '-',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'*') > 0 AND INSTR(V_STR_LEFT,'*') < V_MIN_INDEX, INSTR(V_STR_LEFT,'*'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'*') = V_MIN_INDEX, '*',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'/') > 0 AND INSTR(V_STR_LEFT,'/') < V_MIN_INDEX, INSTR(V_STR_LEFT,'/'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'/') = V_MIN_INDEX, '/',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,'(') > 0 AND INSTR(V_STR_LEFT,'(') < V_MIN_INDEX, INSTR(V_STR_LEFT,'('),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,'(') = V_MIN_INDEX, '(',V_OPERATOR);
SET V_MIN_INDEX = IF(INSTR(V_STR_LEFT,')') > 0 AND INSTR(V_STR_LEFT,')') < V_MIN_INDEX, INSTR(V_STR_LEFT,')'),V_MIN_INDEX);
SET V_OPERATOR = IF(INSTR(V_STR_LEFT,')') = V_MIN_INDEX, ')',V_OPERATOR);
SET V_VALUE = SUBSTRING(V_STR_LEFT,1,INSTR(V_STR_LEFT,V_OPERATOR)-1);
SET V_STR_LEFT= SUBSTRING(V_STR_LEFT, LOCATE(V_OPERATOR,V_STR_LEFT) + 1);
-- SET V_RETURN_STR_RIGHT = CONCAT( V_RETURN_STR_RIGHT ,IF(V_OPERATOR = '(' OR V_OPERATOR = ')' ,V_VALUE,CONCAT(T1,'.',V_VALUE)),V_OPERATOR);
SET V_RETURN_STR_RIGHT = CONCAT( V_RETURN_STR_RIGHT ,IF(LENGTH(V_VALUE) = 0,V_VALUE,CONCAT(T1,'.',V_VALUE)),V_OPERATOR);
SET V_CNT = V_CNT + 1;
-- INSERT INTO EXAMPLE VALUES(V_CNT,V_RETURN_STR,V_OPERATOR,V_STR_RIGHT);
END WHILE;
-- INSERT INTO EXAMPLE VALUES(4,V_RETURN_STR_RIGHT,'','오른쪽');
-- 공식의 종류에 따른 출력값을 결정한다. YqM 인지만 판별 하면됨
SET V_RETURN_STR_INSIDE = IF(V_FORMULA = 'YqM',CONCAT('(',V_STR_INSIDE_T2,'-',V_STR_INSIDE_T1,')')
, CONCAT('\t((\t' ,V_STR_INSIDE_T1 ,'-' ,V_STR_INSIDE_T1 ,')/' ,V_STR_INSIDE_T2 ,')*100\t'));
-- ,'-' ,V_STR_INSIDE_T1 ,')/' ,V_STR_INSIDE_T2 ,')*100' ); // YqM 아니면 다 YoY 로 해석
SET V_RETURN_STR = CONCAT(V_RETURN_STR_LEFT,V_RETURN_STR_INSIDE,V_RETURN_STR_RIGHT);
-- INSERT INTO EXAMPLE VALUES(5,V_RETURN_STR,'','결과');
RETURN V_RETURN_STR;
-- RETURN @value;
END $$
delimiter ;
SELECT YoY('A201130+A201131-(M201131+YqM(M111900+(M216890/M211350)-M112400)+A298300-M112444)','T_FIN_DATA_Y0','T_FIN_DATA_Y1') YoY;
-- SELECT * FROM EXAMPLE;
/*
SET @EMAIL = 'A201130+YqM(M111900+(M216890/M211350)-M112400)+A298300-M112444';
SELECT @EMAIL,SUBSTRING(@email,1,INSTR(@email,'YqM')-1) leftStr,SUBSTRING_INDEX(@email,'YqM',-1) rightStr*/
SELECT YoY('YoY(A201130)','T_FIN_DATA_Y0','T_FIN_DATA_Y1') YoY
union
SELECT YoY('YoY(A201130-M201000)','T_FIN_DATA_Y0','T_FIN_DATA_Y1') YoY
union
SELECT YoY('YqM(A201130)','T_FIN_DATA_Y0','T_FIN_DATA_Y1') YoY;
댓글
댓글 쓰기