/*
 * rc_diffweight_dbio.pgc - 기대금액으로 가중한 차이 평균 (rc_diffweight_dbio), librcdbio.a.
 * ECPG; sum(diff_amt*expected_amt)/nullif(sum(expected_amt),0) 가중평균을 반환.
 */
#include <string.h>
#include <userlog.h>
#include "rc_diffweight_dbio.h"

long rcdb_weighted_diff(const char *bizdate)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    double h_w = 0;
    EXEC SQL END DECLARE SECTION;
    strncpy(h_bizdate, bizdate, sizeof(h_bizdate)-1); h_bizdate[sizeof(h_bizdate)-1] = 0;
    EXEC SQL SELECT coalesce(sum(diff_amt * expected_amt)::float8 / nullif(sum(expected_amt), 0), 0)
        INTO :h_w FROM rc_diff WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("rcdb_weighted_diff FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    return (long) h_w;
}
