/*
 * cm_fxabov_dbio.pgc - cm 모듈 DB 접근 함수 (cm_fxabov_dbio), archived into libcmdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 통화 평균환율을 초과한 환율 레코드 수 (상관 서브쿼리 avg).
 */
#include <string.h>
#include <userlog.h>
#include "cm_fxabov_dbio.h"

long cmdb_fx_above_avg(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_v = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT count(*) INTO :h_v FROM cm_fx_rate f
        WHERE f.base_date = :h_bizdate
          AND f.rate_bps > (SELECT avg(f2.rate_bps) FROM cm_fx_rate f2 WHERE f2.ccy = f.ccy);
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
