Haluaisin tehdä MSSQL:ssä funktion, jolla voisi määrittää kentät ja funktio laskisi näistä sarakkeista tunnusluvun. Esimerkiksi miten tehdään funktio f, joka laskisi alkioista tilastollisen tunnusluvun Cronbachin alfa? Jos vaikka taulussa olisi kentät ky1,...,ky20, ja näistä kaikista haluttaisiin laskea tunnusluku niin funktiota kutsuttaisiin muodossa
f(ky1,ky2,ky3,ky4,ky5,ky6,ky7,ky8,ky9,ky10,ky11,ky12,ky13,ky14,ky15,ky16,ky17,ky18,ky19,ky20)
ja se palauttaisi
DROP PROCEDURE IF EXISTS alpha; GO CREATE PROCEDURE alpha AS BEGIN EXEC sp_execute_external_script @language = N'R' , @script = N'library(psych); a = tryCatch(alpha(Test), warning = function(w) {alpha(Test,check.keys=TRUE)}) str(a)' , @input_data_1 = N'SELECT CAST([ky1] as float) as ky1, CAST([ky2] as float) as ky2, CAST([ky3] as float) as ky3, CAST([ky4] as float) as ky4, CAST([ky5] as float) as ky5, CAST([ky6] as float) as ky6, CAST([ky7] as float) as ky7, CAST([ky8] as float) as ky8, CAST([ky9] as float) as ky9, CAST([ky10] as float) as ky10, CAST([ky11] as float) as ky11, CAST([ky12] as float) as ky12, CAST([ky13] as float) as ky13, CAST([ky14] as float) as ky14, CAST([ky15] as float) as ky15, CAST([ky16] as float) as ky16, CAST([ky17] as float) as ky17, CAST([ky18] as float) as ky18, CAST([ky19] as float) as ky19, CAST([ky20] as float) as ky20 FROM Vuodet' , @input_data_1_name = N'Test' END;
Samoin
f(ky1,ky2,ky3)
palauttaisi
DROP PROCEDURE IF EXISTS alpha; GO CREATE PROCEDURE alpha AS BEGIN EXEC sp_execute_external_script @language = N'R' , @script = N'library(psych); a = tryCatch(alpha(Test), warning = function(w) {alpha(Test,check.keys=TRUE)}) str(a)' , @input_data_1 = N'SELECT CAST([ky1] as float) as ky1, CAST([ky2] as float) as ky2, CAST([ky3] as float) as ky3, FROM Vuodet' , @input_data_1_name = N'Test' END;
f(ky1,ky10,ky20)
palauttaa
DROP PROCEDURE IF EXISTS alpha; GO CREATE PROCEDURE alpha AS BEGIN EXEC sp_execute_external_script @language = N'R' , @script = N'library(psych); a = tryCatch(alpha(Test), warning = function(w) {alpha(Test,check.keys=TRUE)}) str(a)' , @input_data_1 = N'SELECT CAST([ky1] as float) as ky1, CAST([ky10] as float) as ky10, CAST([ky20] as float) as ky20 FROM Vuodet' , @input_data_1_name = N'Test' END;
f(ky1,ky10,ky20,huonokentta)
palauttaa virheen.
Vai miten tuollainen funktio, jossa parametrien määrä ei ole vakio, kannattaa toteuttaa?
Aihe on jo aika vanha, joten et voi enää vastata siihen.