SELECT
    a.CAMPAIGN_ID campaignId,
    a.CAMPAIGN_CODE campaignCode,
    a.CAMPAIGN_NAME campaignName,
    a.CAMPAIGN_TYPE campaignType,
    a.CHANEL chanel,
    a.START_TIME startTime,
    a.END_TIME endTime,
    a.CUSTOMER_NUMBER customerNumber,
    a.STATUS status,
    a.CUSTOMER_NUMBER cusNum,
    b.SLKHThamgiaCD numOfJoinedCus,
    c.SLKHChuaTuongTac numOfNotJoinedCus,
    d.SLKHDoNotCall_Khoa numOfLockCus
FROM CAMPAIGN a
LEFT JOIN (SELECT campaign_id, COUNT (*) AS SLKHThamgiaCD
                  FROM   campaign_customer cc INNER JOIN CUSTOMER cus ON cc.CUSTOMER_ID = cus.CUSTOMER_ID
                  WHERE 1 = 1 AND cc.IN_CAMPAIGN_STATUS = 1 AND cus.STATUS = 1
                  group by campaign_id) b
ON a.CAMPAIGN_ID = b.CAMPAIGN_ID
LEFT JOIN (SELECT campaign_id, COUNT (*) AS SLKHChuaTuongTac
                  FROM   campaign_customer cc INNER JOIN CUSTOMER cus ON cc.CUSTOMER_ID = cus.CUSTOMER_ID
                  WHERE 1 = 1 AND cc.STATUS <> 0 AND cus.STATUS = 1
                  group by campaign_id) c
ON c.CAMPAIGN_ID = a.CAMPAIGN_ID
LEFT JOIN (SELECT cc.campaign_id, count(*) AS SLKHDoNotCall_Khoa
        FROM CAMPAIGN_CUSTOMER cc , CUSTOMER c
        WHERE cc.CUSTOMER_ID = c.CUSTOMER_ID
        AND (c.IPCC_STATUS = 'locked' or c.CALL_ALLOWED = 0) AND cc.STATUS = 1
        GROUP BY cc.CAMPAIGN_ID) d
ON d.CAMPAIGN_ID = a.CAMPAIGN_ID
WHERE 1 = 1
AND COMPANY_SITE_ID = :p_company_site_id
AND a.STATUS <> -1
