変数名と操作を受け取り、sqlで実行するコードを作成する関数を作成する必要があります。私はループ実行テキストを貼り付けるR-関数 - NULLワードを内部に入れよう
vars <- list('vl_Saldo_Prom_Deu_Aco', 'vl_Saldo_Prom_Deu_Exc')
ops <- list('sum', 'max')
:
for (i in vars) {
for (j in ops) {
print(cat(texto(i,j)))
}
}
をそして私が手:
sum(b.vl_Saldo_Prom_Deu_Aco) as vl_Saldo_Prom_Deu_Aco_sum_12,
sum(case when(b.cd_Anio*100+b.cd_Mes)>=a.cd_periodo_observacion_M6 then b.vl_Saldo_Prom_Deu_Aco else 0 end) as vl_Saldo_Prom_Deu_Aco_sum_6,
sum(case when(b.cd_Anio*100+b.cd_Mes)>=a.cd_periodo_observacion_M3 then b.vl_Saldo_Prom_Deu_Aco else 0 end) as vl_Saldo_Prom_Deu_Aco_sum_3,
sum(case when(b.cd_Anio*100+b.cd_Mes)=a.cd_periodo_observacion then b.vl_Saldo_Prom_Deu_Aco else 0 end) as vl_Saldo_Prom_Deu_Aco_sum_1,NULL
max(b.vl_Saldo_Prom_Deu_Aco) as vl_Saldo_Prom_Deu_Aco_max_12,
max(case when(b.cd_Anio*100+b.cd_Mes)>=a.cd_periodo_observacion_M6 then b.vl_Saldo_Prom_Deu_Aco else 0 end) as vl_Saldo_Prom_Deu_Aco_max_6,
max(case when(b.cd_Anio*100+b.cd_Mes)>=a.cd_periodo_observacion_M3 then b.vl_Saldo_Prom_Deu_Aco else 0 end) as vl_Saldo_Prom_Deu_Aco_max_3,
max(case when(b.cd_Anio*100+b.cd_Mes)=a.cd_periodo_observacion then b.vl_Saldo_Prom_Deu_Aco else 0 end) as vl_Saldo_Prom_Deu_Aco_max_1,NULL
sum(b.vl_Saldo_Prom_Deu_Exc) as vl_Saldo_Prom_Deu_Exc_sum_12,
sum(case when(b.cd_Anio*100+b.cd_Mes)>=a.cd_periodo_observacion_M6 then b.vl_Saldo_Prom_Deu_Exc else 0 end) as vl_Saldo_Prom_Deu_Exc_sum_6,
sum(case when(b.cd_Anio*100+b.cd_Mes)>=a.cd_periodo_observacion_M3 then b.vl_Saldo_Prom_Deu_Exc else 0 end) as vl_Saldo_Prom_Deu_Exc_sum_3,
sum(case when(b.cd_Anio*100+b.cd_Mes)=a.cd_periodo_observacion then b.vl_Saldo_Prom_Deu_Exc else 0 end) as vl_Saldo_Prom_Deu_Exc_sum_1,NULL
max(b.vl_Saldo_Prom_Deu_Exc) as vl_Saldo_Prom_Deu_Exc_max_12,
max(case when(b.cd_Anio*100+b.cd_Mes)>=a.cd_periodo_observacion_M6 then b.vl_Saldo_Prom_Deu_Exc else 0 end) as vl_Saldo_Prom_Deu_Exc_max_6,
max(case when(b.cd_Anio*100+b.cd_Mes)>=a.cd_periodo_observacion_M3 then b.vl_Saldo_Prom_Deu_Exc else 0 end) as vl_Saldo_Prom_Deu_Exc_max_3,
max(case when(b.cd_Anio*100+b.cd_Mes)=a.cd_periodo_observacion then b.vl_Saldo_Prom_Deu_Exc else 0 end) as vl_Saldo_Prom_Deu_Exc_max_1,NULL
どの変数と操作のリストで
texto <- function(var,opera) {
paste0(
opera, '(b.', var, ') as ', var, '_', opera, '_12,', '\n',
opera, '(case when(b.cd_Anio*100+b.cd_Mes)>=a.cd_periodo_observacion_M6 then b.', var, ' else 0 end) as ', var,'_', opera,'_6,', '\n',
opera, '(case when(b.cd_Anio*100+b.cd_Mes)>=a.cd_periodo_observacion_M3 then b.', var, ' else 0 end) as ', var,'_', opera,'_3,', '\n',
opera, '(case when(b.cd_Anio*100+b.cd_Mes)=a.cd_periodo_observacion then b.', var, ' else 0 end) as ', var,'_', opera,'_1,'
)
}
:私は、関数を書きましたNULL単語を除いて、ほぼ正常です。私はなぜそれが印刷されているのか、それを避ける方法を知りたいです。