我的代码产生了这个错误:
Stored Procedure: MySQL, Error Code: 1222. The used SELECT statements have a different number of columns 0.015 sec
我试图通过动态查询来展示Pearson&#39。
drop procedure if exists correlation_proc;
DELIMITER //
create procedure correlation_proc
( _tbl varchar(50),
_col1 varchar(50),
_col2 varchar(50)
)
BEGIN
#declare N int;
#declare XY decimal (9,4);
#declare sumX decimal (9,4);
#declare sumY decimal (9,4);
#declare sumXY decimal unsigned;
#declare sumxEXP2 decimal unsigned;
#declare sumyEXP2 decimal unsigned;
declare r decimal (9,4);
declare sql_statement varchar(300);
set @dynamic_query = concat("select count(",@col1,"), sum(",@col1, "), sum(", @col2 , "), sum(",@col1, "*", @col2,"), sum(",@col1, "*", @col1,"), sum(",@col2, "*", @col2,") into @N, @XY, @sumX, @sumY, @sumXY, @sumxEXP2, @sumyEXP2 from ", @tbl, " where ", @col1, " or ", @col2, " IS NOT NULL ;");
set @tbl = _tbl;
set @col1 = _col1;
set @col2 = _col2;
set @dynamic_sql = sql_statement;
# select count(_col1)
# into N
#from _tbl;
#select SUM(col1)
#into sumX
#from _tbl;
#select SUM(col2)
#into sumY
#from _tbl;
#select SUM(_col1*_col1)
#into sumxEXP2
#from _tbl;
#select SUM(_col2*_col2)
#into sumyEXP2
#from _tbl;
#select SUM(_col1*_col2)
#into sumXY
#from _tbl;
set r = (@N*(@sumXY)-(@sumX*@sumY)) / (SQRT((@N*@sumxEXP2 - @sumX*@sumX)*(@N*@sumyEXP2 - @sumY*@sumY)));
prepare select_statement from @dynamic_query;
execute select_statement;
deallocate prepare select_statement;
SELECT 'Correlation Coefficient' AS 'Descriptive Stat', r AS 'Statistic';
END //
delimiter ;
call correlation_proc ("gestationalage_birthweight", "gestational_age", "birth_weight");