/*
 * mg_chdistinct_dbio.pgc - mg 모듈 DB 접근 함수 (mg_chdistinct_dbio), archived into libmgdbio.a.
 * ECPG (EXEC SQL) functions; run on the caller's XA branch. No EXEC SQL CONNECT.
 * 2건 이상 전문이 오간 고유 채널 수 (count DISTINCT + 서브셀렉트).
 */
#include <string.h>
#include <userlog.h>
#include "mg_chdistinct_dbio.h"

long mgdb_chan_mti_distinct(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(DISTINCT channel) INTO :h_v FROM mg_msg_log
        WHERE biz_date = :h_bizdate
          AND channel IN (SELECT channel FROM mg_msg_log
                          WHERE biz_date = :h_bizdate GROUP BY channel HAVING count(*) >= 2);
    if (sqlca.sqlcode < 0) return -1;
    return h_v;
}
