/*
 * rc_amtminmax_dbio.pgc - 대사 금액 스팬 (rc_amtminmax_dbio), librcdbio.a.
 * ECPG; min(amount)/max(amount)/avg(amount) 을 한 번에 구해 평균을 반환, 최소/최대는 출력인자.
 */
#include <string.h>
#include <userlog.h>
#include "rc_amtminmax_dbio.h"

long rcdb_amount_span(const char *bizdate, long *min_out, long *max_out)
{
    EXEC SQL BEGIN DECLARE SECTION;
    char h_bizdate[16];
    long h_min = 0, h_max = 0;
    double h_avg = 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(min(amount),0), coalesce(max(amount),0), coalesce(avg(amount),0)
        INTO :h_min, :h_max, :h_avg FROM rc_match_log WHERE biz_date = :h_bizdate;
    if (sqlca.sqlcode < 0) { userlog("rcdb_amount_span FAIL [%d] %s", sqlca.sqlcode, sqlca.sqlerrm.sqlerrmc); return -1; }
    if (min_out) *min_out = h_min;
    if (max_out) *max_out = h_max;
    return (long) h_avg;
}
