--
-- Table structure for table `anti_malware_optm`
--
CREATE TABLE IF NOT EXISTS `anti_malware_optm` (
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `malware_name` varchar(100) DEFAULT NULL,
  `m_id` varchar(50) DEFAULT NULL,
  `namecnt` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `appctrl_optm_chart`
--
CREATE TABLE IF NOT EXISTS `appctrl_optm_chart` (
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `application_category` blob,
  `app_category_id` int(11) DEFAULT NULL,
  `count` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


--
-- Table structure for table `appctrl_optm_summary`
--
CREATE TABLE IF NOT EXISTS `appctrl_optm_summary` (
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `user_name` varchar(256) DEFAULT NULL,
  `ComputerName` varchar(260) DEFAULT NULL,
  `m_id` varchar(50) DEFAULT NULL,
  `count` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


--
-- Table structure for table `asset_management`
--
CREATE TABLE IF NOT EXISTS `asset_management` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `category_id` int(11) DEFAULT NULL,
  `username` varchar(256) DEFAULT NULL,
  `asset_name` varchar(300) DEFAULT NULL,
  `asset_id` int(11) DEFAULT NULL,
  `details` varchar(512) DEFAULT NULL,
  `m_id` varchar(50) DEFAULT NULL,
  `ComputerName` varchar(260) DEFAULT NULL,
  `DomainName` varchar(256) DEFAULT NULL,
  `GroupName` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `asset_index` (`incident_date`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;


--
-- Table structure for table `auto_update`
--
CREATE TABLE IF NOT EXISTS `auto_update` (
  `last_update_date` DATE DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TRIGGER IF EXISTS trig_DashChart_After_Insert;
DELIMITER $$
CREATE TRIGGER trig_DashChart_After_Insert AFTER INSERT ON auto_update FOR EACH ROW BEGIN  delete from dash_summary;  insert into dash_summary SELECT incident_date, t1.sod as sod, t1.name as name, namecnt, machcnt, t1.charttype, orgtype FROM dash_view t1 INNER JOIN (SELECT sod, MAX(namecnt) as Maxval, charttype FROM dash_view GROUP BY sod, charttype) t2 ON t2.sod = t1.sod AND t2.maxval = t1.namecnt and t2.charttype = t1.charttype where (incident_date >= unix_timestamp(GetCurrentDate() - interval 30 day) and incident_date <= unix_timestamp(GetCurrentDate() + interval 1 day)) GROUP BY t1.sod, charttype;  delete from dash_top_attacks;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 1 from dash_chart where charttype = 1 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 0 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 2 from dash_chart where charttype = 1 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 6 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 3 from dash_chart where charttype = 1 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 14 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 4 from dash_chart where charttype = 1 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 29 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 1 from dash_chart where charttype = 3 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 0 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 2 from dash_chart where charttype = 3 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 6 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 3 from dash_chart where charttype = 3 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 14 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 4 from dash_chart where charttype = 3 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 29 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  END $$
DELIMITER ;

DROP TRIGGER IF EXISTS trig_DashChart_After_Update;
DELIMITER $$
CREATE TRIGGER trig_DashChart_After_Update AFTER UPDATE ON auto_update FOR EACH ROW 
BEGIN  
delete from dash_summary;  
insert into dash_summary SELECT incident_date, t1.sod as sod, t1.name as name, namecnt, machcnt, t1.charttype, orgtype 
FROM dash_view t1 INNER JOIN (SELECT sod, MAX(namecnt) as Maxval, charttype 
FROM dash_view GROUP BY sod, charttype) t2 ON t2.sod = t1.sod AND t2.maxval = t1.namecnt and t2.charttype = t1.charttype 
where (incident_date >= unix_timestamp(GetCurrentDate() - interval 30 day) and incident_date <= unix_timestamp(GetCurrentDate() + interval 1 day)) GROUP BY t1.sod, charttype;  delete from dash_top_attacks;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 1 from dash_chart where charttype = 1 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 0 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 2 from dash_chart where charttype = 1 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 6 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 3 from dash_chart where charttype = 1 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 14 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 4 from dash_chart where charttype = 1 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 29 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 1 from dash_chart where charttype = 3 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 0 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 2 from dash_chart where charttype = 3 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 6 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 3 from dash_chart where charttype = 3 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 14 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  insert into dash_top_attacks select name, count(distinct m_id) as 'machcnt', charttype, orgtype, 4 from dash_chart where charttype = 3 and (incident_date >= unix_timestamp(GetCurrentDate() - interval 29 day) and incident_date < unix_timestamp(GetCurrentDate() + interval 1 day))  group by name order by machcnt DESC limit 5;  END $$
DELIMITER ;

--
-- Table structure for table `cld_delete_rpt_status`
--
CREATE TABLE IF NOT EXISTS `cld_delete_rpt_status` (
  `delete_status` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

TRUNCATE TABLE cld_delete_rpt_status;

INSERT INTO `cld_delete_rpt_status`(`delete_status`) VALUES(0);

--
-- Table structure for table `cloud_dash_status`
--
CREATE TABLE IF NOT EXISTS `cloud_dash_status` (
  `last_sent_date` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

TRUNCATE TABLE cloud_dash_status;

INSERT INTO `cloud_dash_status`(`last_sent_date`) VALUES(0);

--
-- Table structure for table `cloud_status`
--
CREATE TABLE IF NOT EXISTS `cloud_status` (
  `last_sent_date` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

TRUNCATE TABLE cloud_status;

INSERT INTO `cloud_status`(`last_sent_date`) VALUES(0);

--
-- Table structure for table `dash_chart`
--
CREATE TABLE IF NOT EXISTS `dash_chart` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `name` varchar(256) DEFAULT NULL,
  `namecnt` int(11) DEFAULT NULL,
  `ComputerName` varchar(260) DEFAULT NULL,
  `m_id` varchar(50) DEFAULT NULL,
  `charttype` int(11) DEFAULT NULL,
  `orgtype` int(11) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `dash_chart_index` (`incident_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `dash_summary`
--
CREATE TABLE IF NOT EXISTS `dash_summary` (
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `name` varchar(256) DEFAULT NULL,
  `namecnt` int(11) DEFAULT NULL,
  `machcnt` int(11) DEFAULT NULL,
  `charttype` int(11) DEFAULT NULL,
  `orgtype` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `dash_threat_meter`
--
CREATE TABLE IF NOT EXISTS `dash_threat_meter` (
  `incident_hour` int(11) DEFAULT NULL,
  `m_id` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `dash_top_attacks`
--
CREATE TABLE IF NOT EXISTS `dash_top_attacks` (
  `name` varchar(256) DEFAULT NULL,
  `machcnt` int(11) DEFAULT NULL,
  `charttype` int(11) DEFAULT NULL,
  `orgtype` int(11) DEFAULT NULL,
  `lastxdays` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `dashboard_cloud`
--
CREATE TABLE IF NOT EXISTS `dashboard_cloud` (
  `incident_date` int(11) DEFAULT NULL,
  `attack_name` varchar(256) DEFAULT NULL,
  `affected_cnt` int(11) DEFAULT NULL,
  `affected_percent` int(11) DEFAULT NULL,
  `module_type` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `ddos_optm`
--
CREATE TABLE IF NOT EXISTS `ddos_optm` (
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `attack_ip` varchar(256) DEFAULT NULL,
  `ComputerName` varchar(260) DEFAULT NULL,
  `m_id` varchar(50) DEFAULT NULL,
  `attacks_cnt` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `devctrl_optm_chart`
--
CREATE TABLE IF NOT EXISTS `devctrl_optm_chart` (
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `device_type` int(11) DEFAULT NULL,
  `count` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `devctrl_optm_summary`
--
CREATE TABLE IF NOT EXISTS `devctrl_optm_summary` (
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `user` varchar(256) DEFAULT NULL,
  `ComputerName` varchar(260) DEFAULT NULL,
  `m_id` varchar(50) DEFAULT NULL,
  `count` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `dlp_optm_chart`
--
CREATE TABLE IF NOT EXISTS `dlp_optm_chart` (
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `content_type` varchar(100) DEFAULT NULL,
  `content_type_id` int(11) DEFAULT NULL,
  `count` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `dlp_optm_summary`
--
CREATE TABLE IF NOT EXISTS `dlp_optm_summary` (
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `user` varchar(256) DEFAULT NULL,
  `ComputerName` varchar(260) DEFAULT NULL,
  `m_id` varchar(50) DEFAULT NULL,
  `count` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `event_log`
--
CREATE TABLE IF NOT EXISTS `event_log` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `log_date` bigint(20) DEFAULT NULL,
  `log_type` int(11) DEFAULT NULL,
  `message` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `evtlog_index` (`log_date`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `ips_optm`
--
CREATE TABLE IF NOT EXISTS `ips_optm` (
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `vulnerability` varchar(256) DEFAULT NULL,
  `attacks_cnt` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `memory_scan_viruses`
--
CREATE TABLE IF NOT EXISTS `memory_scan_viruses` (
  `incident_hour` int(11) DEFAULT NULL,
  `m_id` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `port_scan_optm`
--
CREATE TABLE IF NOT EXISTS `port_scan_optm` (
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `attack_ip` varchar(256) DEFAULT NULL,
  `m_id` varchar(50) DEFAULT NULL,
  `ComputerName` varchar(260) DEFAULT NULL,
  `attacks_cnt` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_anti_malware_chart_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_anti_malware_chart_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `client_id` bigint(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_anti_malware_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_anti_malware_fact` (
  `malware_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(20) DEFAULT NULL,
  `malware_name` varchar(260) DEFAULT NULL,
  `malware_type` varchar(100) DEFAULT NULL,
  `action_taken` varchar(50) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`malware_id`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_GroupId` (`GroupId`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_app_category_master`
--
CREATE TABLE IF NOT EXISTS `rpt_app_category_master` (
  `app_category_id` int(11) NOT NULL AUTO_INCREMENT,
  `app_category` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`app_category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_appcon_oa_chart_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_appcon_oa_chart_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `app_category_id` int(5) DEFAULT NULL,
  `app_category_name` varchar(256) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_category_id` (`app_category_id`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_appcon_oa_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_appcon_oa_fact` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `application_name` varchar(256) DEFAULT NULL,
  `app_category_id` int(5) DEFAULT NULL,
  `app_category_name` varchar(256) DEFAULT NULL,
  `application_version` varchar(256) DEFAULT NULL,
  `application_path` varchar(1024) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_app_category_id` (`app_category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_appcon_od_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_appcon_od_fact` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `scan_info_id` char(38) DEFAULT NULL,
  `scan_info_type` int(11) DEFAULT NULL,
  `install_date` int(11) DEFAULT NULL,
  `application_name` varchar(256) DEFAULT NULL,
  `application_version` varchar(256) DEFAULT NULL,
  `app_category` varchar(256) DEFAULT NULL,
  `app_path` varchar(256) DEFAULT NULL,
  `publisher` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_scan_info_id` (`scan_info_id`),
  KEY `idx_scan_info_type` (`scan_info_type`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_appcon_od_summarized_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_appcon_od_summarized_fact` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `scan_info_id` char(38) DEFAULT NULL,
  `scan_info_type` int(11) DEFAULT NULL,
  `module` varchar(256) DEFAULT NULL,
  `summary` varchar(256) DEFAULT NULL,
  `unauthorized_app_cnt` int(11) DEFAULT NULL,
  `authorized_app_cnt` int(11) DEFAULT NULL,
  `installed_app_cnt` int(11) DEFAULT NULL,
  `client_id` int(11) DEFAULT NULL,
  `GroupId` int(11) DEFAULT NULL,
  UNIQUE KEY `rowid` (`rowid`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_incident_date` (`incident_date`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_asset_mgnt_chart_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_asset_mgnt_chart_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `category_id` int(2) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_category_id` (`category_id`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_asset_mgnt_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_asset_mgnt_fact` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `asset_id` int(2) NOT NULL,
  `category_id` int(1) DEFAULT NULL,
  `asset_name` varchar(300) DEFAULT NULL,
  `details` varchar(512) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_asset_id` (`asset_id`),
  KEY `idx_category_id` (`category_id`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_ddos_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_ddos_fact` (
  `ddos_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `action_taken` varchar(256) DEFAULT NULL,
  `ddos_info_id` char(40) DEFAULT NULL,
  `attacker_ip` varchar(256) DEFAULT NULL,
  `attacker_mac` varchar(256) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`ddos_id`),
  UNIQUE KEY `ddos_info_id_UNIQUE` (`ddos_info_id`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_GroupId` (`GroupId`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `rpt_ddos_chart_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `client_id` bigint(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_device_chart_summary_1`
--
CREATE TABLE IF NOT EXISTS `rpt_device_chart_summary_1` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL COMMENT 'Reference for user_id in rpt_user_master table.',
  `device_type_id` int(2) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT '0',
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_md5_match` (`md5_match`),
  KEY `idx_device_type_id` (`device_type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_device_chart_summary_2`
--
CREATE TABLE IF NOT EXISTS `rpt_device_chart_summary_2` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `device_id` bigint(20) DEFAULT NULL,
  `device_type_id` int(2) DEFAULT NULL,
  `action_id` int(2) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT '0',
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_device_id` (`device_id`),
  KEY `idx_action_id` (`action_id`),
  KEY `idx_md5_match` (`md5_match`),
  KEY `idx_device_type_id` (`device_type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_device_control_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_device_control_fact` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `device_id` bigint(20) DEFAULT NULL,
  `device_type_id` int(11) DEFAULT NULL,
  `action_id` int(2) DEFAULT NULL COMMENT '1=Access Blocked\n2=Write Access Blocked',
  `user_id` bigint(20) DEFAULT NULL COMMENT 'Reference for user_id in rpt_user_master table.',
  `client_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `IsNotify` boolean DEFAULT FALSE,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_device_id` (`device_id`),
  KEY `idx_device_type_id` (`device_type_id`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_device_master_dim`
--
CREATE TABLE IF NOT EXISTS `rpt_device_master_dim` (
  `device_id` int(11) NOT NULL AUTO_INCREMENT,
  `device_name` varchar(256) DEFAULT NULL,
  `device_type_id` int(11) DEFAULT NULL,
  `serial_number` varchar(256) DEFAULT NULL,
  `manufacturer` varchar(256) DEFAULT NULL,
  `dev_size` int(11) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  PRIMARY KEY (`device_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_dlp_application_master_dim`
--
CREATE TABLE IF NOT EXISTS `rpt_dlp_application_master_dim` (
  `application_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `application_name` varchar(255) DEFAULT NULL,
  `channel_type` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`application_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_dlp_oa_application_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_dlp_oa_application_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `application_id` bigint(20) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT '0',
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_application_id` (`application_id`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_dlp_oa_content_type_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_dlp_oa_content_type_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `channel_type_id` int(2) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `files_copied_count` bigint(20) DEFAULT '0',
  `confidential_data_count` bigint(20) DEFAULT '0',
  `user_define_dictionary_count` bigint(20) DEFAULT '0',
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_channel_id` (`channel_type_id`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_dlp_oa_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_dlp_oa_fact` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `file_path` varchar(1024) DEFAULT NULL,
  `content_type_id` int(11) DEFAULT NULL,
  `content_type` varchar(100) DEFAULT NULL,
  `matched_item_id` bigint(20) DEFAULT NULL,
  `matched_item` varchar(500) DEFAULT NULL,
  `channel_type_id` int(2) DEFAULT NULL,
  `channel_type` varchar(256) DEFAULT NULL,
  `channel_details` varchar(1024) DEFAULT NULL,
  `data_id` varchar(256) DEFAULT NULL,
  `action_id` int(2) DEFAULT NULL,
  `action_taken` varchar(45) DEFAULT NULL,
  `local_app_id` int(11) DEFAULT NULL,
  `application_id` bigint(20) DEFAULT NULL,
  `sender` varchar(60) DEFAULT NULL,
  `receiver` mediumtext DEFAULT NULL,
  `subject` mediumtext DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_content_type_id` (`content_type_id`),
  KEY `idx_channel_id` (`channel_type_id`),
  KEY `idx_application_id` (`application_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_dlp_oa_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_dlp_oa_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `channel_type_id` int(2) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `removable_device_count` bigint(20) DEFAULT '0',
  `application_count` bigint(20) DEFAULT '0',
  `network_share_count` bigint(20) DEFAULT '0',
  `clipboard_count` bigint(20) DEFAULT '0',
  `print_screen_count` bigint(20) DEFAULT '0',
  `print_count` bigint(20) DEFAULT '0',
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_md5_match` (`md5_match`),
  KEY `idx_channel_type_id` (`channel_type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_dlp_od_chart_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_dlp_od_chart_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `scan_info_id` char(38) DEFAULT NULL,
  `scan_start_date` int(11) DEFAULT NULL,
  `content_type_id` int(11) DEFAULT NULL,
  `content_type` varchar(100) DEFAULT NULL,
  `conf_data_cat_id` int(11) DEFAULT NULL,
  `conf_data_cat` varchar(200) DEFAULT NULL,
  `matched_item_id` int(11) DEFAULT NULL,
  `matched_item` varchar(500) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT '0',
  `md5_match` char(64) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_conf_data_cat_id` (`conf_data_cat_id`),
  KEY `idx_content_type_id` (`content_type_id`),
  KEY `idx_matched_item_id` (`matched_item_id`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_dlp_od_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_dlp_od_fact` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `scan_info_id` char(38) DEFAULT NULL,
  `scan_start_date` int(11) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `file_path` varchar(1024) DEFAULT NULL,
  `file_type` varchar(512) DEFAULT NULL,
  `content_type_id` int(11) DEFAULT NULL,
  `content_type` varchar(100) DEFAULT NULL,
  `conf_data_cat_id` int(11) DEFAULT NULL,
  `conf_data_cat` varchar(200) DEFAULT NULL,
  `matched_item_id` int(11) DEFAULT NULL,
  `matched_item` varchar(500) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_scan_info_id` (`scan_info_id`),
  KEY `idx_content_type_id` (`content_type_id`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_matched_item_id` (`matched_item_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_dlp_od_summarized_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_dlp_od_summarized_fact` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `scan_info_id` char(38) NOT NULL DEFAULT '',
  `scan_start_date` int(11) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `scan_type_id` int(2) DEFAULT NULL,
  `scan_type` varchar(256) DEFAULT NULL,
  `confident_data_inc_count` bigint(20) DEFAULT '0',
  `data_dictionary_inc_count` bigint(20) DEFAULT '0',
  PRIMARY KEY (`scan_info_id`),
  UNIQUE KEY `rowid` (`rowid`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_scan_start_date` (`scan_start_date`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_fam_chart_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_fam_chart_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `files_copied_on_local` int(11) DEFAULT '0',
  `files_deleted_on_local` int(11) DEFAULT '0',
  `files_renamed_on_local` int(11) DEFAULT '0',
  `files_copied_on_removable` int(11) DEFAULT '0',
  `files_deleted_on_removable` int(11) DEFAULT '0',
  `files_renamed_on_removable` int(11) DEFAULT '0',
  `files_copied_on_network` int(11) DEFAULT '0',
  `files_deleted_on_network` int(11) DEFAULT '0',
  `files_renamed_on_network` int(11) DEFAULT '0',
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_fam_extn_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_fam_extn_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `extension` varchar(5) DEFAULT NULL,
  `file_extension` varchar(15) DEFAULT NULL,
  `file_ext_chksum` bigint(20) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `files_copied_on_local` int(11) DEFAULT '0',
  `files_deleted_on_local` int(11) DEFAULT '0',
  `files_renamed_on_local` int(11) DEFAULT '0',
  `files_copied_on_removable` int(11) DEFAULT '0',
  `files_deleted_on_removable` int(11) DEFAULT '0',
  `files_renamed_on_removable` int(11) DEFAULT '0',
  `files_copied_on_network` int(11) DEFAULT '0',
  `files_deleted_on_network` int(11) DEFAULT '0',
  `files_renamed_on_network` int(11) DEFAULT '0',
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_md5_match` (`md5_match`),
  KEY `idx_file_ext_chksum` (`file_ext_chksum`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_fam_file_master_dim`
--
CREATE TABLE IF NOT EXISTS `rpt_fam_file_master_dim` (
  `fam_file_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `file_name` varchar(1024) DEFAULT NULL,
  `file_extention` varchar(5) DEFAULT NULL,
  `file_ext_chksum` bigint(20) DEFAULT NULL,
  `file_md5` char(64) DEFAULT NULL,
  PRIMARY KEY (`fam_file_id`),
  KEY `idx_file_md5` (`file_md5`),
  KEY `idx_file_ext_checksum` (`file_ext_chksum`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_fam_incident_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_fam_incident_fact` (
  `fam_incident_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `fam_file_id` bigint(20) DEFAULT NULL,
  `location_id` int(11) DEFAULT NULL COMMENT '1=Local Drive\n2=Removable Drive\n3=Network Drive',
  `event_id` int(11) DEFAULT NULL COMMENT '1=Copy\n2=Delete\n3=Rename',
  `event_details` varchar(2048) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `incident_date` int(11) DEFAULT NULL,
  `file_ext_chksum` bigint(20) DEFAULT NULL,
  `file_extension` varchar(5) DEFAULT NULL,
  PRIMARY KEY (`fam_incident_id`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_location_id` (`location_id`),
  KEY `idx_event_id` (`event_id`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_file_ext_chksum` (`file_ext_chksum`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_firewall_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_firewall_fact` (
  `firewall_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `local_ip` varchar(15) DEFAULT NULL,
  `remote_ip` varchar(15) DEFAULT NULL,
  `protocol` varchar(20) DEFAULT NULL,
  `direction` varchar(50) DEFAULT NULL,
  `firewall_level` varchar(20) DEFAULT NULL,
  `protocol_type` int(11) DEFAULT NULL,
  `local_port` int(11) DEFAULT NULL,
  `remote_port` int(11) DEFAULT NULL,
  `exception_name` varchar(100) DEFAULT NULL,
  `client_id` int(11) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  `GroupId` int(11) DEFAULT NULL,
  PRIMARY KEY (`firewall_id`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_ips_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_ips_fact` (
  `ips_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `vulnerability` varchar(256) DEFAULT NULL,
  `action_taken` varchar(256) DEFAULT NULL,
  `rule_desc` varchar(500) DEFAULT NULL,
  `rule_ref` varchar(500) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`ips_id`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `rpt_ips_chart_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `client_id` bigint(11) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_port_scan_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_port_scan_fact` (
  `port_scan_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `attacker_ip` varchar(256) DEFAULT NULL,
  `attacker_mac` varchar(256) DEFAULT NULL,
  `scanports` varchar(3600) DEFAULT NULL,
  `action_taken` varchar(256) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`port_scan_id`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_GroupId` (`GroupId`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `rpt_port_scan_chart_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `client_id` bigint(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_tuneup_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_tuneup_fact` (
  `tuneup_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `last_date` int(11) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`tuneup_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_virus_chart_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_virus_chart_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `client_id` bigint(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_virus_incident_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_virus_incident_fact` (
  `virus_scan_id` bigint(20) NOT NULL,
  `incident_date` int(11) DEFAULT NULL,
  `virus_incident_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `client_id` int(11) DEFAULT NULL,
  `GroupId` int(11) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  `module_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`virus_incident_id`),
  KEY `idx_virus_scan_id` (`virus_scan_id`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_module_id` (`user_id`),
  KEY `idx_incident_date_only` (`incident_date_only`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_virus_scan_dim`
--
CREATE TABLE IF NOT EXISTS `rpt_virus_scan_dim` (
  `virus_scan_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `infected_file` varchar(17000) DEFAULT NULL,
  `virus_name` varchar(100) DEFAULT NULL,
  `action_taken` varchar(50) DEFAULT NULL,
  `module` varchar(256) DEFAULT NULL,
  `module_id` int(11) DEFAULT NULL,
  `version` varchar(100) DEFAULT NULL,
  `infection_source` varchar(50) DEFAULT NULL,
  `md5_virus_scan` char(64) DEFAULT NULL,
  PRIMARY KEY (`virus_scan_id`),
  KEY `idx_virus_name` (`virus_name`),
  KEY `idx_md5_virus_scan` (`md5_virus_scan`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_vuln_affected_platforms_dim`
--
CREATE TABLE IF NOT EXISTS `rpt_vuln_affected_platforms_dim` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `cve_id` char(14) NOT NULL,
  `platform_name` varchar(256) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_cve_id` (`cve_id`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_vuln_affected_products_dim`
--
CREATE TABLE IF NOT EXISTS `rpt_vuln_affected_products_dim` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `cve_id` char(14) NOT NULL,
  `product_name` varchar(256) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_cve_id` (`cve_id`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_vuln_incident_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_vuln_incident_fact` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `scan_type` varchar(100) DEFAULT NULL,
  `severity` int(2) DEFAULT '0',
  `vendor_id` bigint(20) DEFAULT NULL,
  `vuln_id` bigint(20) DEFAULT NULL,
  `cve_id` char(14) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_severity` (`severity`),
  KEY `idx_vendor_id` (`vendor_id`),
  KEY `idx_vuln_id` (`vuln_id`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_vuln_master_dim`
--
CREATE TABLE IF NOT EXISTS `rpt_vuln_master_dim` (
  `vuln_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `cve_id` char(14) DEFAULT NULL,
  `vuln_title` varchar(256) DEFAULT NULL,
  `severity` int(2) DEFAULT '0',
  `base_metrics` varchar(200) DEFAULT NULL,
  `vendor_id` bigint(20) DEFAULT NULL,
  `description` varchar(5000) DEFAULT NULL,
  `last_modified_date` int(11) DEFAULT NULL,
  `score` varchar(60) DEFAULT NULL,
  `def_version` int(11) DEFAULT NULL,
  PRIMARY KEY (`vuln_id`),
  KEY `idx_cve_id` (`cve_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_vuln_references_dim`
--
CREATE TABLE IF NOT EXISTS `rpt_vuln_references_dim` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `cve_id` char(14) NOT NULL,
  `source_name` varchar(256) DEFAULT NULL,
  `reference_name` varchar(5000) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_cve_id` (`cve_id`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_vuln_severity_chart_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_vuln_severity_chart_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `severity` int(5) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_severity` (`severity`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_vuln_topvuln_chart_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_vuln_topvuln_chart_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `vuln_id` int(5) DEFAULT NULL,
  `cve_id` char(14) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_vuln_id` (`vuln_id`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_vuln_vendor_chart_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_vuln_vendor_chart_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `vendor_id` int(5) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_vendor_id` (`vendor_id`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_vuln_vendor_master_dim`
--
CREATE TABLE IF NOT EXISTS `rpt_vuln_vendor_master_dim` (
  `vendor_id` int(11) NOT NULL AUTO_INCREMENT,
  `vendor_name` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`vendor_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_websec_bolckedsites_dim`
--
CREATE TABLE IF NOT EXISTS `rpt_websec_bolckedsites_dim` (
  `url_id` int(11) NOT NULL AUTO_INCREMENT,
  `website` varchar(1024) DEFAULT NULL,
  `websec_category_id` bigint(20) DEFAULT NULL,
  `website_md5` char(64) DEFAULT NULL,
  PRIMARY KEY (`url_id`),
  KEY `idx_website_md5` (`website_md5`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_websec_category_dim`
--
CREATE TABLE IF NOT EXISTS `rpt_websec_category_dim` (
  `websec_category_id` bigint(20) NOT NULL,
  `category` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`websec_category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_websec_chart_summary`
--
CREATE TABLE IF NOT EXISTS `rpt_websec_chart_summary` (
  `rowid` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date_only` int(11) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `websec_category_id` int(5) DEFAULT NULL,
  `md5_match` char(64) DEFAULT NULL,
  `incident_count` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`rowid`),
  KEY `idx_incident_date_only` (`incident_date_only`),
  KEY `idx_GroupId` (`GroupId`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_websec_category_id` (`websec_category_id`),
  KEY `idx_md5_match` (`md5_match`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `rpt_websec_incident_fact`
--
CREATE TABLE IF NOT EXISTS `rpt_websec_incident_fact` (
  `websec_incident_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `incident_date` int(11) DEFAULT NULL,
  `url_id` bigint(20) DEFAULT NULL,
  `websec_category_id` int(5) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL,
  `GroupId` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`websec_incident_id`),
  KEY `idx_incident_date` (`incident_date`),
  KEY `idx_url_id` (`url_id`),
  KEY `idx_websec_category_id` (`websec_category_id`),
  KEY `idx_client_id` (`client_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_GroupId` (`GroupId`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `user_master`
--
CREATE TABLE IF NOT EXISTS `user_master` (
  `user_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `username` varchar(45) DEFAULT NULL,
  `client_id` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

--
-- Table structure for table `vulnscan_optm_summary`
--
CREATE TABLE IF NOT EXISTS `vulnscan_optm_summary` (
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `cve_id` varchar(14) DEFAULT NULL,
  `severity` int(11) DEFAULT NULL,
  `m_id` varchar(50) DEFAULT NULL,
  `count` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `web_security_optm`
--
CREATE TABLE IF NOT EXISTS `web_security_optm` (
  `incident_date` int(11) DEFAULT NULL,
  `sod` varchar(100) DEFAULT NULL,
  `blocked_url` varchar(1024) DEFAULT NULL,
  `category` varchar(100) DEFAULT NULL,
  `category_id` int(11) DEFAULT NULL,
  `count` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `eps_security_key`
--
CREATE TABLE IF NOT EXISTS `eps_security_key` (
  `key_id` int(11) NOT NULL,
  `key_value` varbinary(80) NOT NULL,
    PRIMARY KEY (`key_id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `sysinfo`
--
CREATE TABLE IF NOT EXISTS `sysinfo` (
  `ClientGuid` varchar(40) DEFAULT NULL,
  `ComputerName` varchar(1050) DEFAULT NULL,
  `DomainName` text,
  `PlatformID` int(11) DEFAULT NULL,
  `OSID` int(11) DEFAULT NULL,
  `OSName` text,
  `OSVersion` text,
  `OSServicePack` int(11) DEFAULT NULL,
  `OSSystemType` text,
  `OSManufacturer` text,
  `OSPartialKey` text,
  `DefaultMailClient` text,
  `DefaultBrowser` text,
  `SystemStartTime` bigint(20) DEFAULT NULL,
  `SystemLastShutDownTime` bigint(20) DEFAULT NULL,
  `BIOSDate` text,
  `BIOSVersion` text,
  `BIOSManufacturer` text,
  `ProcessorCoreCount` int(11) DEFAULT NULL,
  `ProcessorLogicalCoreCount` int(11) DEFAULT NULL,
  `ProcessorName` text,
  `ProcessorFrequency` text,
  `ProcessorManufacturer` text,
  `ProcessorVersion` text,
  `SystemManufacturer` text,
  `SystemModel` text,
  `MBoardName` text,
  `MBoardManufacturer` text,
  `MBoardModel` text,
  `MBoardSerial` text,
  `PhysicalMemorySize` bigint(20) DEFAULT NULL,
  `VirtualMemorySize` bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `installed_programs`
--
CREATE TABLE IF NOT EXISTS `installed_programs` (
 `ClientGuid` varchar(40) DEFAULT NULL,
 `Name` text,
 `Publisher` text,
 `InstalledDate` bigint(20) DEFAULT NULL,
 `ProgramSize` bigint(20) DEFAULT NULL,
 `Version1` bigint(20) DEFAULT NULL,
 `Version2` bigint(20) DEFAULT NULL,
 `Version3` bigint(20) DEFAULT NULL,
 `Version4` bigint(20) DEFAULT NULL,
 `PartialKey` text,
 `LicenseStatus` int(10) unsigned DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
--
-- This key used for product key encryption.
--
  insert into eps_security_key values(1, AES_ENCRYPT('SEQRITELICENSEHISTORYENCKEY','AX0586PRTcjrePORerejfBGNDW'));
--
-- Dumping routines
--

DROP FUNCTION IF EXISTS GetCurrentDate;
DELIMITER $$
CREATE FUNCTION `GetCurrentDate`() RETURNS date
BEGIN  DECLARE CurrDate Date;  set CurrDate = date(convert_tz(now(), "+0:0", 'system'));  return CurrDate;  END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `antimalware_rpt_get_chart_pr`;
DELIMITER $$
CREATE PROCEDURE `antimalware_rpt_get_chart_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT DATE(from_unixtime(incident_date_only)) AS inc_date, SUM(incident_count) AS incident_count
		FROM rpt_anti_malware_chart_summary AS a 
		GROUP BY inc_date ORDER BY a.incident_date_only;
	ELSE
		IF par_client_name <> '' AND par_group_id <> 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT DATE(from_unixtime(incident_date_only)) AS inc_date, SUM(incident_count) AS incident_count
				FROM rpt_anti_malware_chart_summary AS a FORCE INDEX(idx_incident_date_only)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str) AND a.GroupId=par_group_id
				GROUP BY inc_date ORDER BY a.incident_date_only;
			ELSEIF var_client_count = 1 THEN
				SELECT DATE(from_unixtime(incident_date_only)) AS inc_date, SUM(incident_count) AS incident_count
				FROM rpt_anti_malware_chart_summary AS a FORCE INDEX(idx_incident_date_only)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id=var_client_ids_str AND a.GroupId=par_group_id
				GROUP BY inc_date ORDER BY a.incident_date_only;
			END IF;
		ELSEIF par_client_name='' AND par_group_id<>0 THEN
			SELECT DATE(from_unixtime(incident_date_only)) AS inc_date, SUM(incident_count) AS incident_count
			FROM rpt_anti_malware_chart_summary AS a FORCE INDEX(idx_incident_date_only)
			WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND a.GroupId=par_group_id
			GROUP BY inc_date ORDER BY a.incident_date_only;
		ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT DATE(from_unixtime(incident_date_only)) AS inc_date, SUM(incident_count) AS incident_count
				FROM rpt_anti_malware_chart_summary AS a FORCE INDEX(idx_incident_date_only)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str)
				GROUP BY inc_date ORDER BY a.incident_date_only;
			ELSEIF var_client_count = 1 THEN
				SELECT DATE(from_unixtime(incident_date_only)) AS inc_date, SUM(incident_count) AS incident_count
				FROM rpt_anti_malware_chart_summary AS a FORCE INDEX(idx_incident_date_only)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id=var_client_ids_str
				GROUP BY inc_date ORDER BY a.incident_date_only;
			END IF;
		ELSEIF par_client_name='' AND par_group_id = 0 THEN
			SELECT DATE(from_unixtime(incident_date_only)) AS inc_date, SUM(incident_count) AS incident_count
			FROM rpt_anti_malware_chart_summary AS a FORCE INDEX(idx_incident_date_only)
			WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			GROUP BY inc_date ORDER BY a.incident_date_only;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `antimalware_rpt_get_count_pr`;
DELIMITER $$
CREATE PROCEDURE `antimalware_rpt_get_count_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports	
		SELECT IFNULL(SUM(incident_count), 0) AS record_count FROM rpt_anti_malware_chart_summary;
	ELSE
	
		IF par_client_name<> '' AND par_group_id <> 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_anti_malware_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str)
				AND a.GroupId = par_group_id;
			ELSEIF var_client_count = 1 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_anti_malware_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id = var_client_ids_str
				AND a.GroupId = par_group_id;
			END IF;

		ELSEIF par_client_name='' AND par_group_id<>0 THEN
			SELECT IFNULL(SUM(incident_count), 0) AS record_count
			FROM rpt_anti_malware_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
			WHERE
			a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND a.GroupId = par_group_id;

		ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_anti_malware_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str);
			ELSEIF var_client_count = 1 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_anti_malware_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id=var_client_ids_str;
			END IF;
		ELSEIF par_client_name='' AND par_group_id = 0 THEN
			SELECT IFNULL(SUM(incident_count), 0) AS record_count
			FROM rpt_anti_malware_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
			WHERE
			a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			ORDER BY a.incident_date_only DESC;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `antimalware_rpt_get_table_pr`;
DELIMITER $$
CREATE PROCEDURE `antimalware_rpt_get_table_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
	
		SELECT incident_date,
		malware_name, malware_type, action_taken,
		a.client_id, b.computer_name, b.domain_name, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, client_id, malware_name, malware_type, action_taken, incident_date AS incident_date_in_seconds
			FROM rpt_anti_malware_fact AS a 
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS a, client_master AS b
		WHERE 
		a.client_id=b.client_id
		ORDER BY incident_date DESC;
	
	ELSE
	
		IF par_client_name<> '' AND par_group_id <> 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT incident_date,
				malware_name, malware_type, action_taken,
				a.client_id, b.computer_name, b.domain_name, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, malware_name, malware_type, action_taken, incident_date AS incident_date_in_seconds
					FROM rpt_anti_malware_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b
				WHERE 
				a.client_id=b.client_id
				ORDER BY incident_date DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT incident_date,
				malware_name, malware_type, action_taken,
				a.client_id, b.computer_name, b.domain_name, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, malware_name, malware_type, action_taken, incident_date AS incident_date_in_seconds
					FROM rpt_anti_malware_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b
				WHERE 
				a.client_id=b.client_id
				ORDER BY incident_date DESC;
			END IF;

		ELSEIF par_client_name='' AND par_group_id<>0 THEN
			SELECT incident_date,
			malware_name, malware_type, action_taken,
			a.client_id, b.computer_name, b.domain_name, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, client_id, malware_name, malware_type, action_taken, incident_date AS incident_date_in_seconds
				FROM rpt_anti_malware_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, client_master AS b
			WHERE 
			a.client_id=b.client_id
			ORDER BY incident_date DESC;

		ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT incident_date,
				malware_name, malware_type, action_taken,
				a.client_id, b.computer_name, b.domain_name, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, malware_name, malware_type, action_taken, incident_date AS incident_date_in_seconds
					FROM rpt_anti_malware_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					ORDER BY a.incident_date DESC
					LIMIT par_start_index,par_max_records
				) AS a, client_master AS b
				WHERE 
				a.client_id=b.client_id
				ORDER BY incident_date DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT incident_date,
				malware_name, malware_type, action_taken,
				a.client_id, b.computer_name, b.domain_name, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, malware_name, malware_type, action_taken, incident_date AS incident_date_in_seconds
					FROM rpt_anti_malware_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id=var_client_ids_str
					ORDER BY a.incident_date DESC
					LIMIT par_start_index,par_max_records
				) AS a, client_master AS b
				WHERE 
				a.client_id=b.client_id
				ORDER BY incident_date DESC;
			END IF;
		ELSEIF par_client_name='' AND par_group_id = 0 THEN
			SELECT incident_date,
			malware_name, malware_type, action_taken,
			a.client_id, b.computer_name, b.domain_name, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, client_id, malware_name, malware_type, action_taken, incident_date AS incident_date_in_seconds
				FROM rpt_anti_malware_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				ORDER BY a.incident_date DESC
				LIMIT par_start_index,par_max_records
			) AS a, client_master AS b
			WHERE 
			a.client_id=b.client_id
			ORDER BY incident_date DESC;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `appcon_oa_rpt_get_chart_pr`;
DELIMITER $$
CREATE PROCEDURE `appcon_oa_rpt_get_chart_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT app_category_name , SUM(incident_count) AS incident_count
		FROM rpt_appcon_oa_chart_summary AS a
		GROUP BY app_category_id ORDER BY incident_count DESC LIMIT 5;
	ELSE
		IF par_username = '' THEN
			IF par_client_name <> '' AND par_group_id <> 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT app_category_name , SUM(incident_count) AS incident_count
					FROM rpt_appcon_oa_chart_summary AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) AND a.GroupId=par_group_id
					GROUP BY app_category_id ORDER BY incident_count DESC LIMIT 5;
				ELSEIF var_client_count = 1 THEN
					SELECT app_category_name , SUM(incident_count) AS incident_count
					FROM rpt_appcon_oa_chart_summary AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id=var_client_ids_str AND a.GroupId=par_group_id
					GROUP BY app_category_id ORDER BY incident_count DESC LIMIT 5;
				END IF;
				
			ELSEIF par_client_name='' AND par_group_id<>0 THEN
			
				SELECT app_category_name , SUM(incident_count) AS incident_count
				FROM rpt_appcon_oa_chart_summary AS a
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId=par_group_id
				GROUP BY app_category_id ORDER BY incident_count DESC LIMIT 5;
					
			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT app_category_name , SUM(incident_count) AS incident_count
					FROM rpt_appcon_oa_chart_summary AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					GROUP BY app_category_id ORDER BY incident_count DESC LIMIT 5;
				ELSEIF var_client_count = 1 THEN
					SELECT app_category_name , SUM(incident_count) AS incident_count
					FROM rpt_appcon_oa_chart_summary AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id=var_client_ids_str
					GROUP BY app_category_id ORDER BY incident_count DESC LIMIT 5;
				END IF;
				
			ELSEIF par_client_name='' AND par_group_id = 0 THEN

				SELECT app_category_name , SUM(incident_count) AS incident_count
				FROM rpt_appcon_oa_chart_summary AS a
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				GROUP BY app_category_id ORDER BY incident_count DESC LIMIT 5;
			END IF;
		ELSE
		
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
			IF par_client_name <> '' THEN
				
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT app_category_name , SUM(incident_count) AS incident_count
					FROM rpt_appcon_oa_chart_summary AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) AND (par_group_id = 0 OR GroupId=par_group_id)
					AND a.user_id = var_user_id
					GROUP BY app_category_id ORDER BY incident_count DESC LIMIT 5;
				ELSEIF var_client_count = 1 THEN
					SELECT app_category_name , SUM(incident_count) AS incident_count
					FROM rpt_appcon_oa_chart_summary AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id=var_client_ids_str AND (par_group_id = 0 OR GroupId=par_group_id)
					AND a.user_id = var_user_id
					GROUP BY app_category_id ORDER BY incident_count DESC LIMIT 5;
				END IF;
					
			ELSE
				SELECT app_category_name , SUM(incident_count) AS incident_count
				FROM rpt_appcon_oa_chart_summary AS a
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id = 0 OR GroupId=par_group_id)
				AND a.user_id = var_user_id
				GROUP BY app_category_id ORDER BY incident_count DESC LIMIT 5;
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `appcon_oa_rpt_get_count_pr`;
DELIMITER $$
CREATE PROCEDURE `appcon_oa_rpt_get_count_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;	
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT IFNULL(SUM(incident_count), 0) AS record_count FROM rpt_appcon_oa_chart_summary;
	ELSE
	
		IF par_username = '' THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_appcon_oa_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id;
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_appcon_oa_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
			
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_appcon_oa_chart_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_appcon_oa_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_appcon_oa_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
			
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_appcon_oa_chart_summary AS a USE INDEX(idx_incident_date_only)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
				
			END IF;
		ELSE
		
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
			
			IF par_client_name<> '' THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_appcon_oa_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId, idx_user_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.user_id = var_user_id
					AND (par_group_id = 0 OR GroupId=par_group_id);
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_appcon_oa_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId, idx_user_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.user_id = var_user_id
					AND (par_group_id = 0 OR GroupId=par_group_id);
				END IF;
			ELSE
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_appcon_oa_chart_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId, idx_user_id)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.user_id = var_user_id
				AND (par_group_id = 0 OR GroupId=par_group_id);
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `appcon_oa_rpt_get_table_pr`;
DELIMITER $$
CREATE PROCEDURE `appcon_oa_rpt_get_table_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT incident_date, a.client_id, computer_name, domain_name, d.username, 
		application_name, application_version, app_category_name, application_path, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
			application_name, application_version, app_category_name, application_path, incident_date AS incident_date_in_seconds
			FROM rpt_appcon_oa_fact AS a
			ORDER BY a.incident_date DESC  LIMIT par_start_index,par_max_records
		) AS a, client_master AS c, rpt_user_master AS d
		WHERE 
		a.client_id = c.client_id AND a.user_id = d.user_id
		ORDER BY incident_date DESC;
	ELSE
		IF par_username = '' THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, d.username, 
					application_name, application_version, app_category_name, application_path, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
						application_name, application_version, app_category_name, application_path, incident_date AS incident_date_in_seconds
						FROM rpt_appcon_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id = c.client_id AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, d.username, 
					application_name, application_version, app_category_name, application_path, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
						application_name, application_version, app_category_name, application_path, incident_date AS incident_date_in_seconds
						FROM rpt_appcon_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id = c.client_id AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
			
				SELECT incident_date, a.client_id, computer_name, domain_name, d.username, 
				application_name, application_version, app_category_name, application_path, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
					application_name, application_version, app_category_name, application_path, incident_date AS incident_date_in_seconds
					FROM rpt_appcon_oa_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS c, rpt_user_master AS d
				WHERE 
				a.client_id = c.client_id AND a.user_id = d.user_id
				ORDER BY incident_date DESC;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, d.username, 
					application_name, application_version, app_category_name, application_path, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
						application_name, application_version, app_category_name, application_path, incident_date AS incident_date_in_seconds
						FROM rpt_appcon_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id = c.client_id AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, d.username, 
					application_name, application_version, app_category_name, application_path, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
						application_name, application_version, app_category_name, application_path, incident_date AS incident_date_in_seconds
						FROM rpt_appcon_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id = c.client_id AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
			
				SELECT incident_date, a.client_id, computer_name, domain_name, d.username, 
				application_name, application_version, app_category_name, application_path, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
					application_name, application_version, app_category_name, application_path, incident_date AS incident_date_in_seconds
					FROM rpt_appcon_oa_fact AS a
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS c, rpt_user_master AS d
				WHERE 
				a.client_id = c.client_id AND a.user_id = d.user_id
				ORDER BY incident_date DESC;
				
			END IF;
			
		ELSE
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
			IF par_client_name<> '' THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, d.username, 
					application_name, application_version, app_category_name, application_path, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
						application_name, application_version, app_category_name, application_path, incident_date AS incident_date_in_seconds
						FROM rpt_appcon_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_user_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.user_id = var_user_id
						AND (par_group_id = 0 OR GroupId=par_group_id)
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id = c.client_id AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, d.username, 
					application_name, application_version, app_category_name, application_path, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
						application_name, application_version, app_category_name, application_path, incident_date AS incident_date_in_seconds
						FROM rpt_appcon_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_user_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.user_id = var_user_id
						AND (par_group_id = 0 OR GroupId=par_group_id)
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id = c.client_id AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;

			ELSE
			
				SELECT incident_date, a.client_id, computer_name, domain_name, d.username, 
				application_name, application_version, app_category_name, application_path, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
					application_name, application_version, app_category_name, application_path, incident_date AS incident_date_in_seconds
					FROM rpt_appcon_oa_fact AS a USE INDEX(idx_incident_date, idx_GroupId, idx_user_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (par_group_id = 0 OR GroupId=par_group_id)
					AND a.user_id = var_user_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS c, rpt_user_master AS d
				WHERE 
				a.client_id = c.client_id AND a.user_id = d.user_id
				ORDER BY incident_date DESC;
			END IF;
		END IF;
	END IF;	
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `appcon_od_rpt_get_applications_installed_pr`;
DELIMITER $$
CREATE PROCEDURE `appcon_od_rpt_get_applications_installed_pr`(
	IN par_scan_info_id CHAR(38),
	IN par_scan_info_type INT(11),    
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	
	SET time_zone = '+00:00';
	
	IF  par_scan_info_type = 3 THEN
	
		SELECT application_name, application_version, app_category, app_path, from_unixtime(install_date) as install_date, publisher, install_date AS incident_date_in_seconds, a.scan_info_type
		FROM rpt_appcon_od_fact AS a USE INDEX(idx_scan_info_id, idx_scan_info_type)
		WHERE
		a.scan_info_id = par_scan_info_id
		AND (a.scan_info_type = 1 OR a.scan_info_type = 2)
		ORDER BY a.install_date DESC LIMIT par_start_index, par_max_records;
		
    ELSE

		SELECT application_name, application_version, app_category, app_path, from_unixtime(install_date) as install_date, 
		publisher, install_date AS incident_date_in_seconds, a.scan_info_type
		FROM rpt_appcon_od_fact AS a USE INDEX(idx_scan_info_id, idx_scan_info_type)
		WHERE
		a.scan_info_id = par_scan_info_id
		AND a.scan_info_type = par_scan_info_type
		ORDER BY a.install_date DESC LIMIT par_start_index, par_max_records;
		
	END IF;
	
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `appcon_od_rpt_get_count_pr`;
DELIMITER $$
CREATE PROCEDURE `appcon_od_rpt_get_count_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;	
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT IFNULL(COUNT(*), 0) AS record_count FROM rpt_appcon_od_summarized_fact;
	ELSE
		IF par_client_name<> '' AND par_group_id <> 0 THEN
		
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT IFNULL(COUNT(*), 0) AS record_count
				FROM rpt_appcon_od_summarized_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str)
				AND a.GroupId = par_group_id;
			ELSEIF var_client_count = 1 THEN
				SELECT IFNULL(COUNT(*), 0) AS record_count
				FROM rpt_appcon_od_summarized_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id = var_client_ids_str
				AND a.GroupId = par_group_id;
			END IF;

		ELSEIF par_client_name='' AND par_group_id<>0 THEN
		
			SELECT IFNULL(COUNT(*), 0) AS record_count
			FROM rpt_appcon_od_summarized_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
			WHERE
			a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND a.GroupId = par_group_id;

		ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
		
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT IFNULL(COUNT(*), 0) AS record_count
				FROM rpt_appcon_od_summarized_fact AS a USE INDEX(idx_incident_date, idx_client_id)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str);
			ELSEIF var_client_count = 1 THEN
				SELECT IFNULL(COUNT(*), 0) AS record_count
				FROM rpt_appcon_od_summarized_fact AS a USE INDEX(idx_incident_date, idx_client_id)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id = var_client_ids_str;
			END IF;
		ELSEIF par_client_name='' AND par_group_id = 0 THEN
		
			SELECT IFNULL(COUNT(*), 0) AS record_count
			FROM rpt_appcon_od_summarized_fact AS a
			WHERE
			a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
			
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `appcon_od_rpt_get_table_pr`;
DELIMITER $$
CREATE PROCEDURE `appcon_od_rpt_get_table_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;	
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT incident_date, a.client_id, computer_name, domain_name, GroupName AS group_name, 
		scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId,  
			scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date AS incident_date_in_seconds
			FROM rpt_appcon_od_summarized_fact AS a
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS a, groups AS b, client_master AS c
		WHERE 
		a.GroupId = b.GroupId AND a.client_id = c.client_id
		ORDER BY incident_date DESC;
	ELSE
		IF par_client_name<> '' AND par_group_id <> 0 THEN
		
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, GroupName AS group_name, 
				scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId,  
					scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date AS incident_date_in_seconds
					FROM rpt_appcon_od_summarized_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, groups AS b, client_master AS c
				WHERE 
				a.GroupId = b.GroupId AND a.client_id = c.client_id
				ORDER BY incident_date DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, GroupName AS group_name, 
				scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId,  
					scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date AS incident_date_in_seconds
					FROM rpt_appcon_od_summarized_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, groups AS b, client_master AS c
				WHERE 
				a.GroupId = b.GroupId AND a.client_id = c.client_id
				ORDER BY incident_date DESC;
			END IF;

		ELSEIF par_client_name='' AND par_group_id<>0 THEN
		
			SELECT incident_date, a.client_id, computer_name, domain_name, GroupName AS group_name, 
			scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId,  
				scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date AS incident_date_in_seconds
				FROM rpt_appcon_od_summarized_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, groups AS b, client_master AS c
			WHERE 
			a.GroupId = b.GroupId AND a.client_id = c.client_id
			ORDER BY incident_date DESC;

		ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
		
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, GroupName AS group_name, 
				scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId,  
					scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date AS incident_date_in_seconds
					FROM rpt_appcon_od_summarized_fact AS a USE INDEX(idx_incident_date, idx_client_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, groups AS b, client_master AS c
				WHERE 
				a.GroupId = b.GroupId AND a.client_id = c.client_id
				ORDER BY incident_date DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, GroupName AS group_name, 
				scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId,  
					scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date AS incident_date_in_seconds
					FROM rpt_appcon_od_summarized_fact AS a USE INDEX(idx_incident_date, idx_client_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, groups AS b, client_master AS c
				WHERE 
				a.GroupId = b.GroupId AND a.client_id = c.client_id
				ORDER BY incident_date DESC;
			END IF;
		ELSEIF par_client_name='' AND par_group_id = 0 THEN
		
			SELECT incident_date, a.client_id, computer_name, domain_name, GroupName AS group_name, 
			scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId,  
				scan_info_id, scan_info_type, `module`, summary, unauthorized_app_cnt, authorized_app_cnt, installed_app_cnt, incident_date AS incident_date_in_seconds
				FROM rpt_appcon_od_summarized_fact AS a
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, groups AS b, client_master AS c
			WHERE 
			a.GroupId = b.GroupId AND a.client_id = c.client_id
			ORDER BY incident_date DESC;
			
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `assets_rpt_get_chart_summ_pr`;
DELIMITER $$
CREATE PROCEDURE `assets_rpt_get_chart_summ_pr`(
IN par_start_date DATE,
IN par_end_date DATE,
IN par_group_id INT(11),
IN par_client_name VARCHAR(45),
IN par_username VARCHAR(512),
IN par_category_id INT(2)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	IF par_username='' THEN
		IF par_client_name <> '' AND par_group_id <> 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT computer_name, incident_count, a.client_id
				FROM
				(
					SELECT client_id, SUM(incident_count) AS incident_count
					FROM rpt_asset_mgnt_chart_summary AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId=par_group_id
					AND a.category_id = par_category_id
					GROUP BY client_id ORDER BY incident_count DESC LIMIT 5
				) AS a, client_master AS b
				WHERE a.client_id=b.client_id;
			ELSEIF var_client_count = 1 THEN
				SELECT computer_name, incident_count, a.client_id
				FROM
				(
					SELECT client_id, SUM(incident_count) AS incident_count
					FROM rpt_asset_mgnt_chart_summary AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id=var_client_ids_str 
					AND a.GroupId=par_group_id
					AND a.category_id = par_category_id
					GROUP BY client_id ORDER BY incident_count DESC LIMIT 5
				) AS a, client_master AS b
				WHERE a.client_id=b.client_id;	
			END IF;
		ELSEIF par_client_name='' AND par_group_id<>0 THEN
			SELECT computer_name, incident_count, a.client_id
			FROM
			(
				SELECT client_id, SUM(incident_count) AS incident_count
				FROM rpt_asset_mgnt_chart_summary AS a
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId=par_group_id
				AND a.category_id = par_category_id
				GROUP BY client_id ORDER BY incident_count DESC LIMIT 5
			) AS a, client_master AS b
			WHERE a.client_id=b.client_id;
		ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT computer_name, incident_count, a.client_id
				FROM
				(
					SELECT client_id, SUM(incident_count) AS incident_count
					FROM rpt_asset_mgnt_chart_summary AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.category_id = par_category_id
					GROUP BY client_id ORDER BY incident_count DESC LIMIT 5
				) AS a, client_master AS b
				WHERE a.client_id=b.client_id;	
			ELSEIF var_client_count = 1 THEN
				SELECT computer_name, incident_count, a.client_id
				FROM
				(
					SELECT client_id, SUM(incident_count) AS incident_count
					FROM rpt_asset_mgnt_chart_summary AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id=var_client_ids_str
					AND a.category_id = par_category_id
					GROUP BY client_id ORDER BY incident_count DESC LIMIT 5
				) AS a, client_master AS b
				WHERE a.client_id=b.client_id;
			END IF;
		ELSEIF par_client_name='' AND par_group_id = 0 THEN
			SELECT computer_name, incident_count, a.client_id
			FROM
			(
				SELECT client_id, SUM(incident_count) AS incident_count
				FROM rpt_asset_mgnt_chart_summary AS a
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.category_id = par_category_id
				GROUP BY client_id ORDER BY incident_count DESC LIMIT 5
			) AS a, client_master AS b
			WHERE a.client_id=b.client_id;
		END IF;
	ELSE
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
		IF par_client_name <> '' THEN			
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
			FROM client_master WHERE (par_group_id=0 OR GroupId=par_group_id) AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT computer_name, incident_count, a.client_id
				FROM
				(
					SELECT client_id, SUM(incident_count) AS incident_count
					FROM rpt_asset_mgnt_chart_summary AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.user_id=var_user_id
					AND (par_group_id=0 OR a.GroupId=par_group_id)
					AND a.category_id = par_category_id
					GROUP BY client_id ORDER BY incident_count DESC LIMIT 5
				) AS a, client_master AS b
				WHERE a.client_id=b.client_id;
			ELSEIF var_client_count = 1 THEN
				SELECT computer_name, incident_count, a.client_id
				FROM
				(
					SELECT client_id, SUM(incident_count) AS incident_count
					FROM rpt_asset_mgnt_chart_summary AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id=var_client_ids_str 
					AND a.user_id=var_user_id
					AND (par_group_id=0 OR a.GroupId=par_group_id)
					AND a.category_id = par_category_id
					GROUP BY client_id ORDER BY incident_count DESC LIMIT 5
				) AS a, client_master AS b
				WHERE a.client_id=b.client_id;	
			END IF;
		ELSE
			SELECT computer_name, incident_count, a.client_id
			FROM
			(
				SELECT client_id, SUM(incident_count) AS incident_count
				FROM rpt_asset_mgnt_chart_summary AS a
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.user_id=var_user_id
				AND (par_group_id=0 OR a.GroupId=par_group_id)
				AND a.category_id = par_category_id
				GROUP BY client_id ORDER BY incident_count DESC LIMIT 5
			) AS a, client_master AS b
			WHERE a.client_id=b.client_id;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `assets_rpt_get_count_pr`;
DELIMITER $$
CREATE PROCEDURE `assets_rpt_get_count_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_category_id INT(2)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT IFNULL(SUM(incident_count), 0) AS record_count 
		FROM rpt_asset_mgnt_chart_summary AS a
		WHERE a.category_id = par_category_id; -- Condition only for Assets.;
	ELSE
		IF par_username='' THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_asset_mgnt_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND a.category_id = par_category_id;
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_asset_mgnt_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND a.category_id = par_category_id;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_asset_mgnt_chart_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND a.category_id = par_category_id;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_asset_mgnt_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.category_id = par_category_id;
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_asset_mgnt_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.category_id = par_category_id;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_asset_mgnt_chart_summary AS a
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.category_id = par_category_id;
			END IF;
		ELSE
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
			IF par_client_name <> '' THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master WHERE (par_group_id=0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_asset_mgnt_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId, idx_user_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id=0 OR a.GroupId = par_group_id)
					AND a.user_id = var_user_id
					AND a.category_id = par_category_id;
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_asset_mgnt_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId, idx_user_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (par_group_id=0 OR a.GroupId = par_group_id)
					AND a.user_id = var_user_id
					AND a.category_id = par_category_id;
				END IF;
			ELSE
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_asset_mgnt_chart_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId, idx_user_id)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id=0 OR a.GroupId = par_group_id)
				AND a.user_id = var_user_id
				AND a.category_id = par_category_id;
			END IF;
		END IF;
	
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `assets_rpt_get_summ_details_count_pr`;
DELIMITER $$
CREATE PROCEDURE `assets_rpt_get_summ_details_count_pr`(
IN par_start_date DATE,
IN par_end_date DATE,
IN par_group_id INT(11),
IN par_client_id VARCHAR(45),
IN par_username VARCHAR(512), -- from filter
IN par_category_id INT(2)
)
BEGIN
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	SET time_zone = '+00:00';
	
	IF par_username='' THEN
		IF par_group_id <> 0 THEN
			SELECT IFNULL(SUM(incident_count), 0) AS record_count
			FROM rpt_asset_mgnt_chart_summary AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
			WHERE
			a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND a.client_id = par_client_id
			AND a.GroupId = par_group_id
			AND a.category_id = par_category_id;

		ELSEIF par_group_id = 0 THEN
			SELECT IFNULL(SUM(incident_count), 0) AS record_count
			FROM rpt_asset_mgnt_chart_summary AS a USE INDEX(idx_incident_date, idx_client_id)
			WHERE
			a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND a.client_id = par_client_id
			AND a.category_id = par_category_id;
		END IF;
	ELSE
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
		
		SELECT IFNULL(SUM(incident_count), 0) AS record_count
		FROM rpt_asset_mgnt_chart_summary AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_user_id)
		WHERE
		a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
		AND a.client_id = par_client_id
		AND (par_group_id=0 OR a.GroupId = par_group_id)
		AND a.user_id = var_user_id
		AND a.category_id = par_category_id;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `assets_rpt_get_summ_details_pr`;
DELIMITER $$
CREATE PROCEDURE `assets_rpt_get_summ_details_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_id VARCHAR(45),
	IN par_username VARCHAR(512), -- from filter
	IN par_category_id INT(2),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	SET time_zone = '+00:00';
	IF par_username='' THEN
		IF par_group_id <> 0 THEN
			SELECT incident_date, b.username, asset_name, details, GroupId, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, 
				user_id, asset_name, details, GroupId, incident_date AS incident_date_in_seconds
				FROM rpt_asset_mgnt_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id = par_client_id
				AND a.GroupId = par_group_id
				AND a.category_id = par_category_id
				ORDER BY a.incident_date LIMIT par_start_index,par_max_records
			) AS a, rpt_user_master AS b
			WHERE a.user_id=b.user_id
			ORDER BY a.incident_date;

		ELSEIF par_group_id = 0 THEN
			SELECT incident_date, b.username, asset_name, details, GroupId, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, 
				user_id, asset_name, details, GroupId, incident_date AS incident_date_in_seconds
				FROM rpt_asset_mgnt_fact AS a USE INDEX(idx_incident_date, idx_client_id)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id = par_client_id
				AND a.category_id = par_category_id
				ORDER BY a.incident_date LIMIT par_start_index,par_max_records
			) AS a, rpt_user_master AS b
			WHERE a.user_id=b.user_id
			ORDER BY a.incident_date;
		END IF;
	ELSE		
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
		SELECT incident_date, b.username, asset_name, details, GroupId, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, 
			user_id, asset_name, details, GroupId, incident_date AS incident_date_in_seconds
			FROM rpt_asset_mgnt_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_user_id)
			WHERE
			a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND a.client_id = par_client_id
			AND a.user_id = var_user_id
			AND (par_group_id=0 OR a.GroupId = par_group_id)
			AND a.category_id = par_category_id
			ORDER BY a.incident_date LIMIT par_start_index,par_max_records
		) AS a, rpt_user_master AS b
		WHERE a.user_id=b.user_id
		ORDER BY a.incident_date;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `assets_rpt_get_table_pr`;
DELIMITER $$
CREATE PROCEDURE `assets_rpt_get_table_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_category_id INT(2),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT incident_date, a.client_id, computer_name, domain_name, c.username, asset_name, details, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, 
			client_id, a.user_id, asset_name, details, GroupId, incident_date AS incident_date_in_seconds
			FROM rpt_asset_mgnt_fact AS a
			WHERE a.category_id = par_category_id -- Condition only for Assets.
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS a, client_master AS b, rpt_user_master AS c
		WHERE 
		a.client_id=b.client_id AND a.user_id=c.user_id
		ORDER BY incident_date DESC;
	ELSE
		IF par_username='' THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, c.username, asset_name, details, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, 
						client_id, a.user_id, asset_name, details, GroupId, incident_date AS incident_date_in_seconds
						FROM rpt_asset_mgnt_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND a.category_id = par_category_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, c.username, asset_name, details, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, 
						client_id, a.user_id, asset_name, details, GroupId, incident_date AS incident_date_in_seconds
						FROM rpt_asset_mgnt_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						AND a.category_id = par_category_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, c.username, asset_name, details, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, a.user_id, asset_name, details, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_asset_mgnt_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND a.category_id = par_category_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b, rpt_user_master AS c
				WHERE 
				a.client_id=b.client_id AND a.user_id=c.user_id
				ORDER BY incident_date DESC;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, c.username, asset_name, details, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, 
						client_id, a.user_id, asset_name, details, GroupId, incident_date AS incident_date_in_seconds
						FROM rpt_asset_mgnt_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.category_id = par_category_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, c.username, asset_name, details, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, 
						client_id, a.user_id, asset_name, details, GroupId, incident_date AS incident_date_in_seconds
						FROM rpt_asset_mgnt_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.category_id = par_category_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, c.username, asset_name, details, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, a.user_id, asset_name, details, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_asset_mgnt_fact AS a
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.category_id = par_category_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b, rpt_user_master AS c
				WHERE 
				a.client_id=b.client_id AND a.user_id=c.user_id
				ORDER BY incident_date DESC;
			END IF;
	
		ELSE
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
			
			IF par_client_name<> '' THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master WHERE (par_group_id=0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, c.username, asset_name, details, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, 
						client_id, a.user_id, asset_name, details, GroupId, incident_date AS incident_date_in_seconds
						FROM rpt_asset_mgnt_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_user_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND (par_group_id=0 OR a.GroupId = par_group_id)
						AND a.user_id = var_user_id
						AND a.category_id = par_category_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, c.username, asset_name, details, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, 
						client_id, a.user_id, asset_name, details, GroupId, incident_date AS incident_date_in_seconds
						FROM rpt_asset_mgnt_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_user_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND (par_group_id=0 OR a.GroupId = par_group_id)
						AND a.user_id = var_user_id
						AND a.category_id = par_category_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS b, rpt_user_master AS c
					WHERE 
					a.client_id=b.client_id AND a.user_id=c.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSE
				SELECT incident_date, a.client_id, computer_name, domain_name, c.username, asset_name, details, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, a.user_id, asset_name, details, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_asset_mgnt_fact AS a USE INDEX(idx_incident_date, idx_GroupId, idx_user_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (par_group_id=0 OR a.GroupId = par_group_id)
					AND a.user_id = var_user_id
					AND a.category_id = par_category_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b, rpt_user_master AS c
				WHERE 
				a.client_id=b.client_id AND a.user_id=c.user_id
				ORDER BY incident_date DESC;
			END IF;
			
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `country_hos`;
DELIMITER $$
CREATE PROCEDURE `country_hos`()
BEGIN

	SET time_zone = '+00:00';
	
  SELECT * FROM rpt_virus_incident_fact limit 1;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `create_index_if_not_exist`;
DELIMITER $$
CREATE PROCEDURE `create_index_if_not_exist`(in TableName varchar(128), in IndexName varchar(128), in CreateIndexQuery varchar(256))
BEGIN  set @Index_cnt = (select count(*) from information_schema.statistics where table_name = TableName and index_name = IndexName);  IF @Index_cnt = 0 THEN SET @sqlstmt = CreateIndexQuery;  PREPARE st FROM @sqlstmt; EXECUTE st; DEALLOCATE PREPARE st; end if; END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `ddos_rpt_get_count_pr`;
DELIMITER $$
CREATE PROCEDURE `ddos_rpt_get_count_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;	
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN 
		SELECT IFNULL(SUM(incident_count), 0) AS record_count FROM rpt_ddos_chart_summary;
	ELSE
		IF par_client_name<> '' AND par_group_id <> 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_ddos_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str)
				AND a.GroupId = par_group_id;
			ELSEIF var_client_count = 1 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_ddos_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id = var_client_ids_str
				AND a.GroupId = par_group_id;
			END IF;

		ELSEIF par_client_name='' AND par_group_id<>0 THEN
			SELECT IFNULL(SUM(incident_count), 0) AS record_count
			FROM rpt_ddos_chart_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId)
			WHERE
			a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND a.GroupId = par_group_id;

		ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_ddos_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str);
			ELSEIF var_client_count = 1 THEN
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_ddos_chart_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id = var_client_ids_str;
			END IF;
		ELSEIF par_client_name='' AND par_group_id = 0 THEN
			SELECT IFNULL(SUM(incident_count), 0) AS record_count
			FROM rpt_ddos_chart_summary AS a
			WHERE
			a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `ddos_rpt_get_table_pr`;
DELIMITER $$
CREATE PROCEDURE `ddos_rpt_get_table_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, action_taken, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, 
			client_id, attacker_ip, attacker_mac, action_taken, GroupId, incident_date AS incident_date_in_seconds
			FROM rpt_ddos_fact AS a 
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS a, client_master AS b
		WHERE 
		a.client_id=b.client_id
		ORDER BY incident_date DESC;
	ELSE
		IF par_client_name<> '' AND par_group_id <> 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, action_taken, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, attacker_ip, attacker_mac, action_taken, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_ddos_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b
				WHERE 
				a.client_id=b.client_id
				ORDER BY incident_date DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, action_taken, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, attacker_ip, attacker_mac, action_taken, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_ddos_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b
				WHERE 
				a.client_id=b.client_id
				ORDER BY incident_date DESC;
			END IF;

		ELSEIF par_client_name='' AND par_group_id<>0 THEN
			SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, action_taken, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, 
				client_id, attacker_ip, attacker_mac, action_taken, GroupId, incident_date AS incident_date_in_seconds
				FROM rpt_ddos_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, client_master AS b
			WHERE 
			a.client_id=b.client_id
			ORDER BY incident_date DESC;

		ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, action_taken, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, attacker_ip, attacker_mac, action_taken, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_ddos_fact AS a USE INDEX(idx_incident_date, idx_client_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b
				WHERE
				a.client_id=b.client_id
				ORDER BY incident_date DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, action_taken, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, 
					client_id, attacker_ip, attacker_mac, action_taken, GroupId, incident_date AS incident_date_in_seconds
					FROM rpt_ddos_fact AS a USE INDEX(idx_incident_date, idx_client_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS b
				WHERE
				a.client_id=b.client_id
				ORDER BY incident_date DESC;
			END IF;
		ELSEIF par_client_name='' AND par_group_id = 0 THEN
			SELECT incident_date, a.client_id, computer_name, domain_name, attacker_ip, attacker_mac, action_taken, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, 
				client_id, attacker_ip, attacker_mac, action_taken, GroupId, incident_date AS incident_date_in_seconds
				FROM rpt_ddos_fact AS a
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, client_master AS b
			WHERE 
			a.client_id=b.client_id
			ORDER BY incident_date DESC;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `device_rpt_get_chart_pr`;
DELIMITER $$
CREATE PROCEDURE `device_rpt_get_chart_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_device_type_id INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT 
		IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
		IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
		FROM rpt_device_chart_summary_2 AS a;
	ELSE
	
		IF par_username = '' THEN
			IF par_device_type_id <> 0 THEN
			
				IF par_client_name <> '' AND par_group_id <> 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT 
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId=par_group_id
						AND a.device_type_id = par_device_type_id;
					ELSEIF var_client_count = 1 THEN
						SELECT 
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id=var_client_ids_str 
						AND a.GroupId=par_group_id
						AND a.device_type_id = par_device_type_id;
					END IF;
				ELSEIF par_client_name='' AND par_group_id <> 0 THEN
					SELECT 
					IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
					IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId=par_group_id
					AND a.device_type_id = par_device_type_id;
				ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT 
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.device_type_id = par_device_type_id;
					ELSEIF var_client_count = 1 THEN
						SELECT 
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id=var_client_ids_str
						AND a.device_type_id = par_device_type_id;
					END IF;
				ELSEIF par_client_name='' AND par_group_id = 0 THEN
					SELECT 
					IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
					IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.device_type_id = par_device_type_id;
				END IF;
			
			ELSE
				IF par_client_name <> '' AND par_group_id <> 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT 
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId=par_group_id;
					ELSEIF var_client_count = 1 THEN
						SELECT 
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id=var_client_ids_str 
						AND a.GroupId=par_group_id;
					END IF;
				ELSEIF par_client_name='' AND par_group_id <> 0 THEN
					SELECT 
					IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
					IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId=par_group_id;
				ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT 
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str);
					ELSEIF var_client_count = 1 THEN
						SELECT 
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id=var_client_ids_str;
					END IF;
				ELSEIF par_client_name='' AND par_group_id = 0 THEN
					SELECT 
					IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
					IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
				END IF;
			END IF;
		ELSE
		
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
			
			IF par_client_name <> '' THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
					IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id = 0 OR a.GroupId=par_group_id)
					AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
					AND a.user_id = var_user_id;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
					IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id=var_client_ids_str 
					AND (par_group_id = 0 OR a.GroupId=par_group_id)
					AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
					AND a.user_id = var_user_id;
				END IF;
			ELSE
			
				SELECT 
				IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
				IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
				FROM rpt_device_chart_summary_2 AS a
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id = 0 OR a.GroupId=par_group_id)
				AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
				AND a.user_id = var_user_id;
				
			END IF;
		END IF;
		
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `device_rpt_get_chart_summ_1_details_pr`;
DELIMITER $$
CREATE PROCEDURE `device_rpt_get_chart_summ_1_details_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_client_id BIGINT(20),
	IN par_user_id BIGINT(20),
	IN par_device_type_id INT(11),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	
	SET time_zone = '+00:00';
	
	IF par_device_type_id <> 0 THEN
		
		SELECT incident_date, a.client_id, computer_name, device_name, manufacturer, a.device_type_id, serial_number, action_id, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, action_id, user_id, incident_date AS incident_date_in_seconds
			FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_device_type_id, idx_user_id)
			WHERE
			a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND (par_client_id = -1 OR a.client_id = par_client_id)
			AND a.user_id = par_user_id
			AND a.device_type_id = par_device_type_id
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS a, rpt_device_master_dim AS b, client_master AS c
		WHERE 
		a.device_id=b.device_id AND a.client_id=c.client_id
		ORDER BY incident_date DESC;
	
	ELSE	
		
		SELECT incident_date, a.client_id, computer_name, device_name, manufacturer, a.device_type_id, serial_number, action_id, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, action_id, user_id, incident_date AS incident_date_in_seconds
			FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_user_id)
			WHERE
			a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND (par_client_id = -1 OR a.client_id = par_client_id)
			AND a.user_id = par_user_id
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS a, rpt_device_master_dim AS b, client_master AS c
		WHERE 
		a.device_id=b.device_id AND a.client_id=c.client_id
		ORDER BY incident_date DESC;
	
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `device_rpt_get_chart_summ_1_pr`;
DELIMITER $$
CREATE PROCEDURE `device_rpt_get_chart_summ_1_pr`(
IN par_start_date DATE,
IN par_end_date DATE,
IN par_group_id INT(11),
IN par_client_name VARCHAR(45),
IN par_username VARCHAR(512),
IN par_device_type_id INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	IF par_username = '' THEN
		IF par_device_type_id <> 0 THEN
		
		
			IF par_client_name<> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT a.client_id, a.user_id, computer_name, b.username, incident_count
					FROM
					(
						SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
						FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_device_type_id)
						WHERE
						a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND a.device_type_id = par_device_type_id
						GROUP BY user_id
						ORDER BY incident_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE 
					a.user_id=b.user_id AND a.client_id=c.client_id
					ORDER BY incident_count DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT a.client_id, a.user_id, computer_name, b.username, incident_count
					FROM
					(
						SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
						FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_device_type_id)
						WHERE
						a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						AND a.device_type_id = par_device_type_id
						GROUP BY user_id
						ORDER BY incident_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE 
					a.user_id=b.user_id AND a.client_id=c.client_id
					ORDER BY incident_count DESC;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				SELECT a.client_id, a.user_id, computer_name, b.username, incident_count
				FROM
				(
					SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id 
					FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_GroupId, idx_device_type_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND a.device_type_id = par_device_type_id
					GROUP BY user_id
					ORDER BY incident_count DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.user_id=b.user_id AND a.client_id=c.client_id
				ORDER BY incident_count DESC;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT a.client_id, a.user_id, computer_name, b.username, incident_count
					FROM
					(
						SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
						FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_client_id, idx_device_type_id)
						WHERE
						a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.device_type_id = par_device_type_id
						GROUP BY user_id
						ORDER BY incident_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE 
					a.user_id=b.user_id AND a.client_id=c.client_id
					ORDER BY incident_count DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT a.client_id, a.user_id, computer_name, b.username, incident_count
					FROM
					(
						SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
						FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_client_id, idx_device_type_id)
						WHERE
						a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.device_type_id = par_device_type_id
						GROUP BY user_id
						ORDER BY incident_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE 
					a.user_id=b.user_id AND a.client_id=c.client_id
					ORDER BY incident_count DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT a.client_id, a.user_id, computer_name, b.username, incident_count
				FROM
				(
					SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
					FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_device_type_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.device_type_id = par_device_type_id
					GROUP BY user_id
					ORDER BY incident_count DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.user_id=b.user_id AND a.client_id=c.client_id
				ORDER BY incident_count DESC;
			END IF;
		
		ELSE
		
			IF par_client_name<> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT a.client_id, a.user_id, computer_name, b.username, incident_count
					FROM
					(
						SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
						FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						GROUP BY user_id
						ORDER BY incident_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE 
					a.user_id=b.user_id AND a.client_id=c.client_id
					ORDER BY incident_count DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT a.client_id, a.user_id, computer_name, b.username, incident_count
					FROM
					(
						SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
						FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						GROUP BY user_id
						ORDER BY incident_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE 
					a.user_id=b.user_id AND a.client_id=c.client_id
					ORDER BY incident_count DESC;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				SELECT a.client_id, a.user_id, computer_name, b.username, incident_count
				FROM
				(
					SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
					FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_GroupId)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					GROUP BY user_id
					ORDER BY incident_count DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.user_id=b.user_id AND a.client_id=c.client_id
				ORDER BY incident_count DESC;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT a.client_id, a.user_id, computer_name, b.username, incident_count
					FROM
					(
						SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
						FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						GROUP BY user_id
						ORDER BY incident_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE 
					a.user_id=b.user_id AND a.client_id=c.client_id
					ORDER BY incident_count DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT a.client_id, a.user_id, computer_name, b.username, incident_count
					FROM
					(
						SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
						FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						GROUP BY user_id
						ORDER BY incident_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE 
					a.user_id=b.user_id AND a.client_id=c.client_id
					ORDER BY incident_count DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT a.client_id, a.user_id, computer_name, b.username, incident_count
				FROM
				(
					SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
					FROM rpt_device_chart_summary_1 AS a
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					GROUP BY user_id
					ORDER BY incident_count DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.user_id=b.user_id AND a.client_id=c.client_id
				ORDER BY incident_count DESC;
			END IF;
		
		END IF;
		
	ELSE
	
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
		
		IF par_client_name<> '' THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
			FROM client_master 
			WHERE (par_group_id = 0 OR GroupId = par_group_id) AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT a.client_id, b.user_id, computer_name, b.username, incident_count
				FROM
				(
					SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
					FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_device_type_id, idx_user_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
					AND a.user_id = var_user_id
					ORDER BY incident_count DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.user_id=b.user_id AND a.client_id=c.client_id
				ORDER BY incident_count DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT a.client_id, b.user_id, computer_name, b.username, incident_count
				FROM
				(
					SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
					FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_device_type_id, idx_user_id)
					WHERE
					a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
					AND a.user_id = var_user_id
					ORDER BY incident_count DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE 
				a.user_id=b.user_id AND a.client_id=c.client_id
				ORDER BY incident_count DESC;
			END IF;
			
		ELSE
		
			SELECT a.client_id, b.user_id, computer_name, b.username, incident_count
			FROM
			(
				SELECT IFNULL(SUM(incident_count), 0) AS incident_count, client_id, a.user_id
				FROM rpt_device_chart_summary_1 AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_device_type_id, idx_user_id)
				WHERE
				a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id = 0 OR a.GroupId = par_group_id)
				AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
				AND a.user_id = var_user_id
				ORDER BY incident_count DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE 
			a.user_id=b.user_id AND a.client_id=c.client_id
			ORDER BY incident_count DESC;
			
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `device_rpt_get_chart_summ_2_details_pr`;
DELIMITER $$
CREATE PROCEDURE `device_rpt_get_chart_summ_2_details_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_device_type_id INT(11), -- Compulsory.
	IN par_action_id INT(11), 	-- Compulsory.
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	IF par_username = '' THEN
		IF par_client_name<> '' AND par_group_id <> 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT incident_date, a.client_id, computer_name, manufacturer, d.username, device_name, 
				a.device_type_id, serial_number, action_id, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, 
					device_type_id, action_id, user_id, incident_date AS incident_date_in_seconds
					FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_device_type_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND a.device_type_id = par_device_type_id
					AND a.action_id = par_action_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
				WHERE 
				a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id=d.user_id
				ORDER BY incident_date DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT incident_date, a.client_id, computer_name, manufacturer, d.username, device_name, 
				a.device_type_id, serial_number, action_id, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, 
					device_type_id, action_id, user_id, incident_date AS incident_date_in_seconds
					FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_device_type_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.GroupId = par_group_id
					AND a.device_type_id = par_device_type_id
					AND a.action_id = par_action_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
				WHERE 
				a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id=d.user_id
				ORDER BY incident_date DESC;	
			END IF;

		ELSEIF par_client_name='' AND par_group_id<>0 THEN
			SELECT incident_date, a.client_id, computer_name, manufacturer, d.username, device_name, 
			a.device_type_id, serial_number, action_id, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, 
				device_type_id, action_id, user_id, incident_date AS incident_date_in_seconds
				FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_GroupId, idx_device_type_id)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND a.device_type_id = par_device_type_id
				AND a.action_id = par_action_id
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
			WHERE 
			a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id=d.user_id
			ORDER BY incident_date DESC;

		ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT incident_date, a.client_id, computer_name, manufacturer, d.username, device_name, 
				a.device_type_id, serial_number, action_id, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, 
					device_type_id, action_id, user_id, incident_date AS incident_date_in_seconds
					FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_device_type_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.device_type_id = par_device_type_id
					AND a.action_id = par_action_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
				WHERE 
				a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id=d.user_id
				ORDER BY incident_date DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT incident_date, a.client_id, computer_name, manufacturer, d.username, device_name, 
				a.device_type_id, serial_number, action_id, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, 
					device_type_id, action_id, user_id, incident_date AS incident_date_in_seconds
					FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_device_type_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.device_type_id = par_device_type_id
					AND a.action_id = par_action_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
				WHERE 
				a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id=d.user_id
				ORDER BY incident_date DESC;	
			END IF;
		ELSEIF par_client_name='' AND par_group_id = 0 THEN
			SELECT incident_date, a.client_id, computer_name, manufacturer, d.username, device_name, 
			a.device_type_id, serial_number, action_id, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, 
					device_type_id, action_id, user_id, incident_date AS incident_date_in_seconds
					FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_device_type_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.device_type_id = par_device_type_id
					AND a.action_id = par_action_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
				WHERE 
				a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id=d.user_id
				ORDER BY incident_date DESC;
		END IF;

	ELSE
	
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
		
		IF par_client_name<> '' THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
			FROM client_master 
			WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT incident_date, a.client_id, computer_name, manufacturer, d.username, device_name, 
				a.device_type_id, serial_number, action_id, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, 
					device_type_id, action_id, user_id, incident_date AS incident_date_in_seconds
					FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId, idx_device_type_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id = 0 OR GroupId=par_group_id)
					AND a.device_type_id = par_device_type_id
					AND a.action_id = par_action_id
					AND a.user_id = var_user_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
				WHERE 
				a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id=d.user_id
				ORDER BY incident_date DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT incident_date, a.client_id, computer_name, manufacturer, d.username, device_name, 
				a.device_type_id, serial_number, action_id, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, 
					device_type_id, action_id, user_id, incident_date AS incident_date_in_seconds
					FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId, idx_device_type_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (par_group_id = 0 OR GroupId=par_group_id)
					AND a.device_type_id = par_device_type_id
					AND a.action_id = par_action_id
					AND a.user_id = var_user_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
				WHERE 
				a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id=d.user_id
				ORDER BY incident_date DESC;	
			END IF;
			
		ELSE
		
			SELECT incident_date, a.client_id, computer_name, manufacturer, d.username, device_name, 
			a.device_type_id, serial_number, action_id, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, 
				device_type_id, action_id, user_id, incident_date AS incident_date_in_seconds
				FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_GroupId, idx_device_type_id)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id = 0 OR GroupId=par_group_id)
				AND a.device_type_id = par_device_type_id
				AND a.action_id = par_action_id
				AND a.user_id = var_user_id
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
			WHERE 
			a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id=d.user_id
			ORDER BY incident_date DESC;
			
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `device_rpt_get_chart_summ_2_pr`;
DELIMITER $$
CREATE PROCEDURE `device_rpt_get_chart_summ_2_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_device_type_id INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF par_username = '' THEN
		IF par_device_type_id <> 0 THEN
		
			IF par_client_name <> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
					FROM
					(
						SELECT 
						device_type_id,
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId=par_group_id
						AND a.device_type_id = par_device_type_id
						GROUP BY device_type_id
						ORDER BY access_blocked_record_count DESC
					) AS a
					ORDER BY access_blocked_record_count DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
					FROM
					(
						SELECT 
						device_type_id,
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id=var_client_ids_str 
						AND a.GroupId=par_group_id
						AND a.device_type_id = par_device_type_id
						GROUP BY device_type_id
						ORDER BY access_blocked_record_count DESC
					) AS a
					ORDER BY access_blocked_record_count DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id <> 0 THEN
				SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
				FROM
				(
					SELECT 
					device_type_id,
					IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
					IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId=par_group_id
					AND a.device_type_id = par_device_type_id
					GROUP BY device_type_id
					ORDER BY access_blocked_record_count DESC
				) AS a
				ORDER BY access_blocked_record_count DESC;
					
			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
					FROM
					(
						SELECT 
						device_type_id,
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.device_type_id = par_device_type_id
						GROUP BY device_type_id
						ORDER BY access_blocked_record_count DESC
					) AS a
					ORDER BY access_blocked_record_count DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
					FROM
					(
						SELECT 
						device_type_id,
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id=var_client_ids_str
						AND a.device_type_id = par_device_type_id
						GROUP BY device_type_id
						ORDER BY access_blocked_record_count DESC
					) AS a
					ORDER BY access_blocked_record_count DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
				FROM
				(
					SELECT 
					device_type_id,
					IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
					IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.device_type_id = par_device_type_id
					GROUP BY device_type_id
					ORDER BY access_blocked_record_count DESC
				) AS a
				ORDER BY access_blocked_record_count DESC;
			END IF;
		
		ELSE
			IF par_client_name <> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
					FROM
					(
						SELECT 
						device_type_id,
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId=par_group_id
						GROUP BY device_type_id
						ORDER BY access_blocked_record_count DESC
					) AS a
					ORDER BY access_blocked_record_count DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
					FROM
					(
						SELECT 
						device_type_id,
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id=var_client_ids_str 
						AND a.GroupId=par_group_id
						GROUP BY device_type_id
						ORDER BY access_blocked_record_count DESC
					) AS a
					ORDER BY access_blocked_record_count DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id <> 0 THEN
				SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
				FROM
				(
					SELECT 
					device_type_id,
					IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
					IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId=par_group_id
					GROUP BY device_type_id
					ORDER BY access_blocked_record_count DESC
				) AS a
				ORDER BY access_blocked_record_count DESC;
			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
					FROM
					(
						SELECT 
						device_type_id,
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						GROUP BY device_type_id
						ORDER BY access_blocked_record_count DESC
					) AS a
					ORDER BY access_blocked_record_count DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
					FROM
					(
						SELECT 
						device_type_id,
						IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
						IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id=var_client_ids_str
						GROUP BY device_type_id
						ORDER BY access_blocked_record_count DESC
					) AS a
					ORDER BY access_blocked_record_count DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
				FROM
				(
					SELECT 
					device_type_id,
					IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
					IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					GROUP BY device_type_id
					ORDER BY access_blocked_record_count DESC
				) AS a
				ORDER BY access_blocked_record_count DESC;
			END IF;
		END IF;

	ELSE
	
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
		
		IF par_client_name <> '' THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
			FROM client_master 
			WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
				FROM
				(
					SELECT 
					device_type_id,
					IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
					IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id = 0 OR a.GroupId=par_group_id)
					AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
					AND a.user_id = var_user_id
					GROUP BY device_type_id
					ORDER BY access_blocked_record_count DESC
				) AS a
				ORDER BY access_blocked_record_count DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
				FROM
				(
					SELECT 
					device_type_id,
					IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
					IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id=var_client_ids_str 
					AND (par_group_id = 0 OR a.GroupId=par_group_id)
					AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
					AND a.user_id = var_user_id
					GROUP BY device_type_id
					ORDER BY access_blocked_record_count DESC
				) AS a
				ORDER BY access_blocked_record_count DESC;
			END IF;
			
		ELSE
		
			SELECT device_type_id, access_blocked_record_count, write_access_blocked_record_count
			FROM
			(
				SELECT 
				device_type_id,
				IFNULL(SUM(CASE WHEN action_id = 1 THEN incident_count ELSE 0 END), 0) AS access_blocked_record_count,
				IFNULL(SUM(CASE WHEN action_id = 2 THEN incident_count ELSE 0 END), 0) AS write_access_blocked_record_count
				FROM rpt_device_chart_summary_2 AS a
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id = 0 OR a.GroupId=par_group_id)
				AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
				AND a.user_id = var_user_id
				GROUP BY device_type_id
				ORDER BY access_blocked_record_count DESC
			) AS a
			ORDER BY access_blocked_record_count DESC;
			
		END IF;
	END IF;
	
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `device_rpt_get_count_pr`;
DELIMITER $$
CREATE PROCEDURE `device_rpt_get_count_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_device_type_id INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		
		SELECT IFNULL(SUM(incident_count), 0) AS record_count FROM rpt_device_chart_summary_2;
		
	ELSE
	
		IF par_username = '' THEN
			IF par_device_type_id <> 0 THEN
			
				IF par_client_name <> '' AND par_group_id <> 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT IFNULL(SUM(incident_count), 0) AS record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId=par_group_id
						AND a.device_type_id = par_device_type_id;
					ELSEIF var_client_count = 1 THEN
						SELECT IFNULL(SUM(incident_count), 0) AS record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id=var_client_ids_str 
						AND a.GroupId=par_group_id
						AND a.device_type_id = par_device_type_id;
					END IF;
				ELSEIF par_client_name='' AND par_group_id <> 0 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId=par_group_id;
				ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT IFNULL(SUM(incident_count), 0) AS record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.device_type_id = par_device_type_id;	
					ELSEIF var_client_count = 1 THEN
						SELECT IFNULL(SUM(incident_count), 0) AS record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id=var_client_ids_str
						AND a.device_type_id = par_device_type_id;
					END IF;
				ELSEIF par_client_name='' AND par_group_id = 0 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.device_type_id = par_device_type_id;
				END IF;
			
			ELSE
				IF par_client_name <> '' AND par_group_id <> 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT IFNULL(SUM(incident_count), 0) AS record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId=par_group_id;
					ELSEIF var_client_count = 1 THEN
						SELECT IFNULL(SUM(incident_count), 0) AS record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id=var_client_ids_str 
						AND a.GroupId=par_group_id;
					END IF;
				ELSEIF par_client_name='' AND par_group_id <> 0 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId=par_group_id;
				ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT IFNULL(SUM(incident_count), 0) AS record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str);
					ELSEIF var_client_count = 1 THEN
						SELECT IFNULL(SUM(incident_count), 0) AS record_count
						FROM rpt_device_chart_summary_2 AS a
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id=var_client_ids_str;
					END IF;
					
				ELSEIF par_client_name='' AND par_group_id = 0 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
				END IF;
			END IF;
		
		ELSE
		
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
			
			IF par_client_name <> '' THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master 
				WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id = 0 OR GroupId=par_group_id)
					AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
					AND a.user_id = var_user_id;
				ELSEIF var_client_count = 1 THEN
					SELECT IFNULL(SUM(incident_count), 0) AS record_count
					FROM rpt_device_chart_summary_2 AS a
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id=var_client_ids_str 
					AND (par_group_id = 0 OR GroupId=par_group_id)
					AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
					AND a.user_id = var_user_id;
				END IF;
				
			ELSE
			
				SELECT IFNULL(SUM(incident_count), 0) AS record_count
				FROM rpt_device_chart_summary_2 AS a
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id = 0 OR GroupId=par_group_id)
				AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
				AND a.user_id = var_user_id;
				
			END IF;			
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `device_rpt_get_table_pr`;
DELIMITER $$
CREATE PROCEDURE `device_rpt_get_table_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_device_type_id INT(11),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		
		SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
		a.device_type_id, serial_number, action_id, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
			action_id, user_id, incident_date AS incident_date_in_seconds
			FROM rpt_device_control_fact AS a 
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
		WHERE 
		a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
		ORDER BY incident_date DESC;
		
	ELSE
	
		IF par_username = '' THEN
			IF par_device_type_id <> 0 THEN
			
				IF par_client_name<> '' AND par_group_id <> 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
						a.device_type_id, serial_number, action_id, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
							action_id, user_id, incident_date AS incident_date_in_seconds
							FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_device_type_id)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND a.GroupId = par_group_id
							AND a.device_type_id = par_device_type_id
							ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
						) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
						WHERE 
						a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
						ORDER BY incident_date DESC;
					ELSEIF var_client_count = 1 THEN
						SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
						a.device_type_id, serial_number, action_id, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
							action_id, user_id, incident_date AS incident_date_in_seconds
							FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_device_type_id)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND a.GroupId = par_group_id
							AND a.device_type_id = par_device_type_id
							ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
						) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
						WHERE 
						a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
						ORDER BY incident_date DESC;
					END IF;

				ELSEIF par_client_name='' AND par_group_id<>0 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
					a.device_type_id, serial_number, action_id, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
						action_id, user_id, incident_date AS incident_date_in_seconds
						FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_GroupId, idx_device_type_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.GroupId = par_group_id
						AND a.device_type_id = par_device_type_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
					WHERE 
					a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
					ORDER BY incident_date DESC;

				ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
						a.device_type_id, serial_number, action_id, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
							action_id, user_id, incident_date AS incident_date_in_seconds
							FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_device_type_id)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND a.device_type_id = par_device_type_id
							ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
						) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
						WHERE 
						a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
						ORDER BY incident_date DESC;
					ELSEIF var_client_count = 1 THEN
						SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
						a.device_type_id, serial_number, action_id, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
							action_id, user_id, incident_date AS incident_date_in_seconds
							FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_device_type_id)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND a.device_type_id = par_device_type_id
							ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
						) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
						WHERE 
						a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
						ORDER BY incident_date DESC;
					END IF;
				ELSEIF par_client_name='' AND par_group_id = 0 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
					a.device_type_id, serial_number, action_id, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
							action_id, user_id, incident_date AS incident_date_in_seconds
							FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_device_type_id)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.device_type_id = par_device_type_id
							ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
						) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
						WHERE 
						a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
						ORDER BY incident_date DESC;
				END IF;
			
			ELSE
			
				IF par_client_name<> '' AND par_group_id <> 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
						a.device_type_id, serial_number, action_id, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
							action_id, user_id, incident_date AS incident_date_in_seconds
							FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND a.GroupId = par_group_id
							ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
						) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
						WHERE 
						a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
						ORDER BY incident_date DESC;
					ELSEIF var_client_count = 1 THEN
						SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
						a.device_type_id, serial_number, action_id, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
							action_id, user_id, incident_date AS incident_date_in_seconds
							FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND a.GroupId = par_group_id
							ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
						) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
						WHERE 
						a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
						ORDER BY incident_date DESC;
					END IF;

				ELSEIF par_client_name='' AND par_group_id<>0 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
					a.device_type_id, serial_number, action_id, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
						action_id, user_id, incident_date AS incident_date_in_seconds
						FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.GroupId = par_group_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
					WHERE 
					a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
					ORDER BY incident_date DESC;

				ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
						a.device_type_id, serial_number, action_id, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
							action_id, user_id, incident_date AS incident_date_in_seconds
							FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
						) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
						WHERE 
						a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
						ORDER BY incident_date DESC;
					ELSEIF var_client_count = 1 THEN
						SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
						a.device_type_id, serial_number, action_id, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
							action_id, user_id, incident_date AS incident_date_in_seconds
							FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
						) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
						WHERE 
						a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
						ORDER BY incident_date DESC;
					END IF;
				ELSEIF par_client_name='' AND par_group_id = 0 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
					a.device_type_id, serial_number, action_id, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
						action_id, user_id, incident_date AS incident_date_in_seconds
						FROM rpt_device_control_fact AS a
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
					WHERE 
					a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;
			
			END IF;
		
		ELSE
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username=par_username LIMIT 1;
			IF par_client_name<> '' THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
					a.device_type_id, serial_number, action_id, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
						action_id, user_id, incident_date AS incident_date_in_seconds
						FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_device_type_id, idx_user_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND (par_group_id = 0 OR GroupId=par_group_id)
						AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
						AND a.user_id = var_user_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
					WHERE 
					a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
					a.device_type_id, serial_number, action_id, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
						action_id, user_id, incident_date AS incident_date_in_seconds
						FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_device_type_id, idx_user_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND (par_group_id = 0 OR GroupId=par_group_id)
						AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
						AND a.user_id = var_user_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
					WHERE 
					a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;
				
			ELSE
			
				SELECT incident_date, a.client_id, computer_name, domain_name, d.username, device_name, 
				a.device_type_id, serial_number, action_id, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, device_id, device_type_id, 
					action_id, user_id, incident_date AS incident_date_in_seconds
					FROM rpt_device_control_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_device_type_id, idx_user_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (par_group_id = 0 OR GroupId=par_group_id)
					AND (par_device_type_id = 0 OR a.device_type_id = par_device_type_id)
					AND a.user_id = var_user_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, rpt_device_master_dim AS b, client_master AS c, rpt_user_master AS d
				WHERE 
				a.device_id=b.device_id AND a.client_id=c.client_id AND a.user_id = d.user_id
				ORDER BY incident_date DESC;
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_oa_rpt_get_chart_pr` ;
DELIMITER $$
CREATE PROCEDURE `dlp_oa_rpt_get_chart_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_channel_id INT(2)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT 
		IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
		IFNULL(SUM(application_count), 0) AS application_count,
		IFNULL(SUM(network_share_count), 0) AS network_share_count,
		IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
		IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
		IFNULL(SUM(print_count), 0) AS print_count
		
		FROM rpt_dlp_oa_summary AS a;
	ELSE
	
		IF par_username = '' THEN
			IF par_channel_id <> 0 THEN
			
				IF par_client_name <> '' AND par_group_id <> 0 THEN
				
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
						IFNULL(SUM(application_count), 0) AS application_count,
						IFNULL(SUM(network_share_count), 0) AS network_share_count,
						IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
						IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
						IFNULL(SUM(print_count), 0) AS print_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND a.channel_type_id = par_channel_id;
					ELSEIF var_client_count = 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
						IFNULL(SUM(application_count), 0) AS application_count,
						IFNULL(SUM(network_share_count), 0) AS network_share_count,
						IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
						IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
						IFNULL(SUM(print_count), 0) AS print_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str 
						AND a.GroupId = par_group_id
						AND a.channel_type_id = par_channel_id;
					END IF;
					
				ELSEIF par_client_name='' AND par_group_id<>0 THEN
				
					SELECT 
					IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
					IFNULL(SUM(application_count), 0) AS application_count,
					IFNULL(SUM(network_share_count), 0) AS network_share_count,
					IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
					IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
					IFNULL(SUM(print_count), 0) AS print_count
					
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND a.channel_type_id = par_channel_id;
					
				ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
						IFNULL(SUM(application_count), 0) AS application_count,
						IFNULL(SUM(network_share_count), 0) AS network_share_count,
						IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
						IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
						IFNULL(SUM(print_count), 0) AS print_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.channel_type_id = par_channel_id;
					ELSEIF var_client_count = 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
						IFNULL(SUM(application_count), 0) AS application_count,
						IFNULL(SUM(network_share_count), 0) AS network_share_count,
						IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
						IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
						IFNULL(SUM(print_count), 0) AS print_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.channel_type_id = par_channel_id;
					END IF;
					
				ELSEIF par_client_name='' AND par_group_id = 0 THEN
				
					SELECT 
					IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
					IFNULL(SUM(application_count), 0) AS application_count,
					IFNULL(SUM(network_share_count), 0) AS network_share_count,
					IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
					IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
					IFNULL(SUM(print_count), 0) AS print_count
					
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.channel_type_id = par_channel_id;
					
				END IF;
				
			ELSE
				
				IF par_client_name <> '' AND par_group_id <> 0 THEN
			
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
						IFNULL(SUM(application_count), 0) AS application_count,
						IFNULL(SUM(network_share_count), 0) AS network_share_count,
						IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
						IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
						IFNULL(SUM(print_count), 0) AS print_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id;
					ELSEIF var_client_count = 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
						IFNULL(SUM(application_count), 0) AS application_count,
						IFNULL(SUM(network_share_count), 0) AS network_share_count,
						IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
						IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
						IFNULL(SUM(print_count), 0) AS print_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str 
						AND a.GroupId = par_group_id;
					END IF;
					
				ELSEIF par_client_name='' AND par_group_id<>0 THEN
				
					SELECT 
					IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
					IFNULL(SUM(application_count), 0) AS application_count,
					IFNULL(SUM(network_share_count), 0) AS network_share_count,
					IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
					IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
					IFNULL(SUM(print_count), 0) AS print_count
					
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id;
					
				ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
						IFNULL(SUM(application_count), 0) AS application_count,
						IFNULL(SUM(network_share_count), 0) AS network_share_count,
						IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
						IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
						IFNULL(SUM(print_count), 0) AS print_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str);
					ELSEIF var_client_count = 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
						IFNULL(SUM(application_count), 0) AS application_count,
						IFNULL(SUM(network_share_count), 0) AS network_share_count,
						IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
						IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
						IFNULL(SUM(print_count), 0) AS print_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str;
					END IF;
					
				ELSEIF par_client_name='' AND par_group_id = 0 THEN
				
					SELECT 
					IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
					IFNULL(SUM(application_count), 0) AS application_count,
					IFNULL(SUM(network_share_count), 0) AS network_share_count,
					IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
					IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
					IFNULL(SUM(print_count), 0) AS print_count
					
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
					
				END IF;
				
			END IF;
			
		ELSE
		
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
			
			IF par_client_name <> '' THEN
				
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
					IFNULL(SUM(application_count), 0) AS application_count,
					IFNULL(SUM(network_share_count), 0) AS network_share_count,
					IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
					IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
					IFNULL(SUM(print_count), 0) AS print_count
					
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND (par_channel_id = 0 OR a.channel_type_id = par_channel_id)
					AND a.user_id = var_user_id;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
					IFNULL(SUM(application_count), 0) AS application_count,
					IFNULL(SUM(network_share_count), 0) AS network_share_count,
					IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
					IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
					IFNULL(SUM(print_count), 0) AS print_count
					
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND (par_channel_id = 0 OR a.channel_type_id = par_channel_id)
					AND a.user_id = var_user_id;
				END IF;
				
			ELSE
				SELECT 
				IFNULL(SUM(removable_device_count), 0) AS removable_device_count,
				IFNULL(SUM(application_count), 0) AS application_count,
				IFNULL(SUM(network_share_count), 0) AS network_share_count,
				IFNULL(SUM(clipboard_count), 0) AS clipboard_count,
				IFNULL(SUM(print_screen_count), 0) AS print_screen_count,
				IFNULL(SUM(print_count), 0) AS print_count
				
				FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_user_id, idx_GroupId)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id = 0 OR a.GroupId = par_group_id)
				AND (par_channel_id = 0 OR a.channel_type_id = par_channel_id)
				AND a.user_id = var_user_id;
				
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_oa_rpt_get_chart_summary_1_details_pr`;
DELIMITER $$
CREATE PROCEDURE `dlp_oa_rpt_get_chart_summary_1_details_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_user_id BIGINT(20),
	IN par_client_id BIGINT(20),
	IN par_channel_type_id INT(11),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	
	SET time_zone = '+00:00';
	
	IF par_channel_type_id <> 0 THEN
	
		IF par_group_id <> 0 THEN
		
			SELECT 
			incident_date, computer_name, domain_name, d.username, file_path, content_type, matched_item, 
			channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
			FROM 
			(
				SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
				file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken, incident_date AS incident_date_in_seconds, sender, receiver, subject
				FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_client_id = -1 OR a.client_id = par_client_id)
				AND a.user_id = par_user_id
				AND a.GroupId = par_group_id
				AND a.channel_type_id = par_channel_type_id
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, client_master AS c, rpt_user_master AS d
			WHERE 
			a.client_id=c.client_id
			AND a.user_id = d.user_id
			ORDER BY incident_date DESC;

		ELSEIF par_group_id = 0 THEN
		
			SELECT 
			incident_date, computer_name, domain_name, d.username, file_path, content_type, matched_item, 
			channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
				file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken, incident_date AS incident_date_in_seconds, sender, receiver, subject
				FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_client_id = -1 OR a.client_id = par_client_id)
				AND a.user_id = par_user_id
				AND a.channel_type_id = par_channel_type_id
				ORDER BY a.incident_date DESC
				LIMIT par_start_index,par_max_records
			) AS a, client_master AS c, rpt_user_master AS d
			WHERE 
			a.client_id=c.client_id
			AND a.user_id = d.user_id
			ORDER BY incident_date DESC;
			
		END IF;
		
	ELSEIF par_channel_type_id = 0 THEN
	
		IF par_group_id <> 0 THEN
			
			SELECT 
			incident_date, computer_name, domain_name, d.username, file_path, content_type, matched_item, 
			channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
				file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken, incident_date AS incident_date_in_seconds, sender, receiver, subject
				FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_client_id = -1 OR a.client_id = par_client_id)
				AND a.user_id = par_user_id
				AND a.GroupId = par_group_id
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, client_master AS c, rpt_user_master AS d
			WHERE 
			a.client_id=c.client_id
			AND a.user_id = d.user_id
			ORDER BY incident_date DESC;

		ELSEIF par_group_id = 0 THEN
		
			SELECT 
			incident_date, computer_name, domain_name, d.username, file_path, content_type, matched_item, 
			channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, client_id, GroupId, user_id, 
				file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken, incident_date AS incident_date_in_seconds, sender, receiver, subject
				FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_client_id = -1 OR a.client_id = par_client_id)
				AND a.user_id = par_user_id
				ORDER BY a.incident_date DESC
				LIMIT par_start_index,par_max_records
			) AS a, client_master AS c, rpt_user_master AS d
			WHERE 
			a.client_id=c.client_id
			AND a.user_id = d.user_id
			ORDER BY incident_date DESC;
			
		END IF;
		
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_oa_rpt_get_chart_summary_1_pr`;
DELIMITER $$
CREATE PROCEDURE `dlp_oa_rpt_get_chart_summary_1_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_channel_id INT(2)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	IF par_username = '' THEN
	
		IF par_channel_id <> 0 THEN
		
			IF par_client_name <> '' AND par_group_id <> 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
					FROM
					(
						SELECT 
						user_id, client_id,
						SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND a.channel_type_id = par_channel_id
						GROUP BY user_id
						ORDER BY record_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE a.user_id = b.user_id AND a.client_id = c.client_id
					ORDER BY record_count DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
					FROM
					(
						SELECT 
						user_id, client_id,
						SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str 
						AND a.GroupId = par_group_id
						AND a.channel_type_id = par_channel_id
						GROUP BY user_id
						ORDER BY record_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE a.user_id = b.user_id AND a.client_id = c.client_id
					ORDER BY record_count DESC;
				END IF;
				
			ELSEIF par_client_name='' AND par_group_id<>0 THEN
			
				SELECT 
				a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
				FROM
				(
					SELECT 
					user_id, client_id,
					SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
			
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId, idx_user_id)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND a.channel_type_id = par_channel_id
					GROUP BY user_id
					ORDER BY record_count DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE a.user_id = b.user_id AND a.client_id = c.client_id
				ORDER BY record_count DESC;
				
			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
					FROM
					(
						SELECT 
						user_id, client_id,
						SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.channel_type_id = par_channel_id
						GROUP BY user_id
						ORDER BY record_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE a.user_id = b.user_id AND a.client_id = c.client_id
					ORDER BY record_count DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
					FROM
					(
						SELECT 
						user_id, client_id,
						SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.channel_type_id = par_channel_id
						GROUP BY user_id
						ORDER BY record_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE a.user_id = b.user_id AND a.client_id = c.client_id
					ORDER BY record_count DESC;
				END IF;
				
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
			
				SELECT 
				a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
				FROM
				(
					SELECT 
					user_id, client_id,
					SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
				
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_user_id)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.channel_type_id = par_channel_id
					GROUP BY user_id
					ORDER BY record_count DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE a.user_id = b.user_id AND a.client_id = c.client_id
				ORDER BY record_count DESC;
			
			END IF;
			
		ELSE
			
			IF par_client_name <> '' AND par_group_id <> 0 THEN
		
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
					FROM
					(
						SELECT 
						user_id, client_id,
						SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
					
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						GROUP BY user_id
						ORDER BY record_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE a.user_id = b.user_id AND a.client_id = c.client_id
					ORDER BY record_count DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
					FROM
					(
						SELECT 
						user_id, client_id,
						SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
					
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str 
						AND a.GroupId = par_group_id
						GROUP BY user_id
						ORDER BY record_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE a.user_id = b.user_id AND a.client_id = c.client_id
					ORDER BY record_count DESC;
				END IF;
				
			ELSEIF par_client_name='' AND par_group_id<>0 THEN
			
				SELECT 
				a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
				FROM
				(
					SELECT 
					user_id, client_id,
					SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
				
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId, idx_user_id)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					GROUP BY user_id
					ORDER BY record_count DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE a.user_id = b.user_id AND a.client_id = c.client_id
				ORDER BY record_count DESC;
				
			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
						SELECT 
					a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
					FROM
					(
						SELECT 
						user_id, client_id,
						SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
					
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						GROUP BY user_id
						ORDER BY record_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE a.user_id = b.user_id AND a.client_id = c.client_id
					ORDER BY record_count DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
					FROM
					(
						SELECT 
						user_id, client_id,
						SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
					
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						GROUP BY user_id
						ORDER BY record_count DESC LIMIT 5
					) AS a, rpt_user_master AS b, client_master AS c
					WHERE a.user_id = b.user_id AND a.client_id = c.client_id
					ORDER BY record_count DESC;
				END IF;
				
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
			
				SELECT 
				a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
				FROM
				(
					SELECT 
					user_id, client_id,
					SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
				
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_user_id)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					GROUP BY user_id
					ORDER BY record_count DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE a.user_id = b.user_id AND a.client_id = c.client_id
				ORDER BY record_count DESC;
				
			END IF;
			
		END IF;

	ELSE
	
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
		
		IF par_client_name <> '' THEN
			
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
			FROM client_master 
			WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT 
				a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
				FROM
				(
					SELECT 
					user_id, client_id,
					SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
					
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND (par_channel_id = 0 OR a.channel_type_id = par_channel_id)
					AND a.user_id = var_user_id
					ORDER BY record_count DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE a.user_id = b.user_id AND a.client_id = c.client_id
				ORDER BY record_count DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT 
				a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
				FROM
				(
					SELECT 
					user_id, client_id,
					SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
					
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND (par_channel_id = 0 OR a.channel_type_id = par_channel_id)
					AND a.user_id = var_user_id
					ORDER BY record_count DESC LIMIT 5
				) AS a, rpt_user_master AS b, client_master AS c
				WHERE a.user_id = b.user_id AND a.client_id = c.client_id
				ORDER BY record_count DESC;
			END IF;
		
		ELSE
		
			SELECT 
			a.user_id, a.client_id, b.username, c.str_ip_address, c.computer_name, record_count
			FROM
			(
				SELECT 
				user_id, client_id,
				SUM(IFNULL(removable_device_count, 0) + IFNULL(application_count, 0) + IFNULL(network_share_count, 0) + IFNULL(clipboard_count, 0) + IFNULL(print_screen_count, 0) + IFNULL(print_count, 0)) AS record_count
				
				FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_user_id, idx_GroupId)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id = 0 OR a.GroupId = par_group_id)
				AND (par_channel_id = 0 OR a.channel_type_id = par_channel_id)
				AND a.user_id = var_user_id
				ORDER BY record_count DESC LIMIT 5
			) AS a, rpt_user_master AS b, client_master AS c
			WHERE a.user_id = b.user_id AND a.client_id = c.client_id
			ORDER BY record_count DESC;
			
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_oa_rpt_get_chart_summary_2_details_pr` ;
DELIMITER $$
CREATE PROCEDURE `dlp_oa_rpt_get_chart_summary_2_details_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_channel_type_id INT(11),
	IN par_content_type_id INT(11),	-- Compulsory
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	IF par_username = '' THEN
	
		IF par_channel_type_id <> 0 THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND a.channel_type_id = par_channel_type_id
						AND a.content_type_id = par_content_type_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c, rpt_user_master AS d
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						AND a.channel_type_id = par_channel_type_id
						AND a.content_type_id = par_content_type_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c, rpt_user_master AS d
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				SELECT 
				incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
				channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
					channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
					FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND a.channel_type_id = par_channel_type_id
					AND a.content_type_id = par_content_type_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS t1, client_master AS c, rpt_user_master AS d 
				WHERE 
				t1.client_id=c.client_id
				AND t1.user_id = d.user_id
				ORDER BY incident_date DESC;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.channel_type_id = par_channel_type_id
						AND a.content_type_id = par_content_type_id
						ORDER BY a.incident_date DESC
						LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.channel_type_id = par_channel_type_id
						AND a.content_type_id = par_content_type_id
						ORDER BY a.incident_date DESC
						LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT 
				incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
				channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
					channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
					FROM rpt_dlp_oa_fact AS a FORCE INDEX(idx_incident_date) 
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.channel_type_id = par_channel_type_id
					AND a.content_type_id = par_content_type_id
					ORDER BY a.incident_date DESC
					LIMIT par_start_index,par_max_records
				) AS t1, client_master AS c, rpt_user_master AS d 
				WHERE 
				t1.client_id=c.client_id
				AND t1.user_id = d.user_id
				ORDER BY incident_date DESC;
			END IF;
		ELSEIF par_channel_type_id = 0 THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND a.content_type_id = par_content_type_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c, rpt_user_master AS d
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						AND a.content_type_id = par_content_type_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c, rpt_user_master AS d
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				SELECT 
				incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
				channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
					channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
					FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND a.content_type_id = par_content_type_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records				
				) AS t1, client_master AS c, rpt_user_master AS d 
				WHERE 
				t1.client_id=c.client_id
				AND t1.user_id = d.user_id
				ORDER BY incident_date DESC;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.content_type_id = par_content_type_id
						ORDER BY a.incident_date DESC
						LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.content_type_id = par_content_type_id
						ORDER BY a.incident_date DESC
						LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c, rpt_user_master AS d 
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT 
				incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
				channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
					channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
					FROM rpt_dlp_oa_fact AS a FORCE INDEX(idx_incident_date) 
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.content_type_id = par_content_type_id
					ORDER BY a.incident_date DESC
					LIMIT par_start_index,par_max_records
				) AS t1, client_master AS c, rpt_user_master AS d
				WHERE 
				t1.client_id=c.client_id
				AND t1.user_id = d.user_id
				ORDER BY incident_date DESC;
			END IF;
		END IF;

	ELSE
		
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
		
		IF par_client_name<> '' THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
			FROM client_master 
			WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT 
				incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
				channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
					channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
					FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND (par_channel_type_id = 0 OR a.channel_type_id = par_channel_type_id)
					AND a.user_id = var_user_id
					AND a.content_type_id = par_content_type_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS t1, client_master AS c, rpt_user_master AS d
				WHERE 
				t1.client_id=c.client_id
				AND t1.user_id = d.user_id
				ORDER BY incident_date DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT 
				incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
				channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
					channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
					FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id,idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND (par_channel_type_id = 0 OR a.channel_type_id = par_channel_type_id)
					AND a.user_id = var_user_id
					AND a.content_type_id = par_content_type_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS t1, client_master AS c, rpt_user_master AS d
				WHERE 
				t1.client_id=c.client_id
				AND t1.user_id = d.user_id
				ORDER BY incident_date DESC;
			END IF;
		
		ELSE
		
			SELECT 
			incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
			channel_type_id, channel_type, channel_details, action_id, action_taken, t1.client_id, incident_date_in_seconds, sender, receiver, subject
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
				channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
				FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_user_id, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id = 0 OR a.GroupId = par_group_id)
				AND (par_channel_type_id = 0 OR a.channel_type_id = par_channel_type_id)
				AND a.user_id = var_user_id
				AND a.content_type_id = par_content_type_id
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS t1, client_master AS c, rpt_user_master AS d
			WHERE 
			t1.client_id=c.client_id
			AND t1.user_id = d.user_id
			ORDER BY incident_date DESC;
		END IF;
	END IF;
	
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_oa_rpt_get_chart_summary_2_pr` ;
DELIMITER $$
CREATE PROCEDURE `dlp_oa_rpt_get_chart_summary_2_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_channel_id INT(2)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	IF par_username = '' THEN
		IF par_channel_id <> 0 THEN
		
			IF par_client_name <> '' AND par_group_id <> 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
					IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
					IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
					
					FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					AND a.channel_type_id = par_channel_id;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
					IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
					IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
					
					FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND a.GroupId = par_group_id
					AND a.channel_type_id = par_channel_id;
				END IF;
				
			ELSEIF par_client_name='' AND par_group_id<>0 THEN
			
				SELECT 
				IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
				IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
				IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
				
				FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				AND a.channel_type_id = par_channel_id;
				
			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
					IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
					IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
					
					FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.channel_type_id = par_channel_id;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
					IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
					IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
					
					FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND a.channel_type_id = par_channel_id;
				END IF;
				
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
			
				SELECT 
				IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
				IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
				IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
				
				FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.channel_type_id = par_channel_id;
				
			END IF;
			
		ELSE
			
			IF par_client_name <> '' AND par_group_id <> 0 THEN
		
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
					IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
					IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
					
					FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
					IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
					IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
					
					FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND a.GroupId = par_group_id;
				END IF;
				
			ELSEIF par_client_name='' AND par_group_id<>0 THEN
			
				SELECT 
				IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
				IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
				IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
				
				FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id;
				
			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
					IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
					IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
					
					FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str);
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
					IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
					IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
					
					FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str;
				END IF;
				
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
			
				SELECT 
				IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
				IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
				IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
				
				FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
				
			END IF;
			
		END IF;

	ELSE
	
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
		
		IF par_client_name <> '' THEN
			
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT 
				IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
				IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
				IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
				
				FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND FIND_IN_SET(a.client_id, var_client_ids_str)
				AND (par_group_id = 0 OR a.GroupId = par_group_id)
				AND (par_channel_id = 0 OR a.channel_type_id = par_channel_id)
				AND a.user_id = var_user_id;
			ELSEIF var_client_count = 1 THEN
				SELECT 
				IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
				IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
				IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
				
				FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.client_id = var_client_ids_str 
				AND (par_group_id = 0 OR a.GroupId = par_group_id)
				AND (par_channel_id = 0 OR a.channel_type_id = par_channel_id)
				AND a.user_id = var_user_id;
			END IF;
		
		ELSE
		
			SELECT 
			IFNULL(SUM(files_copied_count), 0) AS files_copied_count,
			IFNULL(SUM(confidential_data_count), 0) AS confidential_data_count,
			IFNULL(SUM(user_define_dictionary_count), 0) AS user_define_dictionary_count
			
			FROM rpt_dlp_oa_content_type_summary AS a USE INDEX(idx_incident_date_only, idx_user_id, idx_GroupId)
			WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
			AND (par_group_id = 0 OR a.GroupId = par_group_id)
			AND (par_channel_id = 0 OR a.channel_type_id = par_channel_id)
			AND a.user_id = var_user_id;
			
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_oa_rpt_get_chart_summary_3_details_pr` ;
DELIMITER $$
CREATE PROCEDURE `dlp_oa_rpt_get_chart_summary_3_details_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_channel_type_id INT(11),
	IN par_application_id INT(11),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	IF par_username = '' THEN
		IF par_channel_type_id <> 0 THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND a.channel_type_id = par_channel_type_id
						AND a.application_id = par_application_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						AND a.channel_type_id = par_channel_type_id
						AND a.application_id = par_application_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				SELECT 
				incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
				channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
					channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
					FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND a.channel_type_id = par_channel_type_id
					AND a.application_id = par_application_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS c, rpt_user_master AS d 
				WHERE 
				a.client_id=c.client_id
				AND a.user_id = d.user_id
				ORDER BY incident_date DESC;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.channel_type_id = par_channel_type_id
						AND a.application_id = par_application_id
						ORDER BY a.incident_date DESC
						LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d 
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.channel_type_id = par_channel_type_id
						AND a.application_id = par_application_id
						ORDER BY a.incident_date DESC
						LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d 
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT 
				incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
				channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
					channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
					FROM rpt_dlp_oa_fact AS a FORCE INDEX(idx_incident_date) 
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.channel_type_id = par_channel_type_id
					AND a.application_id = par_application_id
					ORDER BY a.incident_date DESC
					LIMIT par_start_index,par_max_records
				) AS a, client_master AS c, rpt_user_master AS d 
				WHERE 
				a.client_id=c.client_id
				AND a.user_id = d.user_id
				ORDER BY incident_date DESC;
			END IF;
		ELSEIF par_channel_type_id = 0 THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND a.application_id = par_application_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						AND a.application_id = par_application_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				SELECT 
				incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
				channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
					channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
					FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND a.application_id = par_application_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records				
				) AS a, client_master AS c, rpt_user_master AS d 
				WHERE 
				a.client_id=c.client_id
				AND a.user_id = d.user_id
				ORDER BY incident_date DESC;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.application_id = par_application_id
						ORDER BY a.incident_date DESC
						LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d 
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
					channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.application_id = par_application_id
						ORDER BY a.incident_date DESC
						LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d 
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT 
				incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
				channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
					channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
					FROM rpt_dlp_oa_fact AS a FORCE INDEX(idx_incident_date) 
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.application_id = par_application_id
					ORDER BY a.incident_date DESC
					LIMIT par_start_index,par_max_records
				) AS a, client_master AS c, rpt_user_master AS d 
				WHERE 
				a.client_id=c.client_id
				AND a.user_id = d.user_id
				ORDER BY incident_date DESC;
			END IF;
		END IF;

	ELSE
	
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
		
		IF par_client_name<> '' THEN
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
			FROM client_master 
			WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT 
				incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
				channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
					channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
					FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND (par_channel_type_id = 0 OR a.channel_type_id = par_channel_type_id)
					AND a.user_id = var_user_id
					AND a.application_id = par_application_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS c, rpt_user_master AS d
				WHERE 
				a.client_id=c.client_id
				AND a.user_id = d.user_id
				ORDER BY incident_date DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT 
				incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
				channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
					channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
					FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND (par_channel_type_id = 0 OR a.channel_type_id = par_channel_type_id)
					AND a.user_id = var_user_id
					AND a.application_id = par_application_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS c, rpt_user_master AS d
				WHERE 
				a.client_id=c.client_id
				AND a.user_id = d.user_id
				ORDER BY incident_date DESC;
			END IF;
		
		ELSE
			
			SELECT 
			incident_date, computer_name, domain_name, d.username, str_ip_address, file_path, content_type, matched_item, 
			channel_type_id, channel_type, channel_details, action_id, action_taken, a.client_id, incident_date_in_seconds, sender, receiver, subject
			FROM
			(
				SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, file_path,content_type,matched_item,
				channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, incident_date AS incident_date_in_seconds, sender, receiver, subject
				FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_user_id, idx_GroupId)
				WHERE
				a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id = 0 OR a.GroupId = par_group_id)
				AND (par_channel_type_id = 0 OR a.channel_type_id = par_channel_type_id)
				AND a.user_id = var_user_id
				AND a.application_id = par_application_id
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, client_master AS c, rpt_user_master AS d
			WHERE 
			a.client_id=c.client_id
			AND a.user_id = d.user_id
			ORDER BY incident_date DESC;
			
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_oa_rpt_get_chart_summary_3_pr` ;
DELIMITER $$
CREATE PROCEDURE `dlp_oa_rpt_get_chart_summary_3_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	IF par_username = '' THEN	
		IF par_client_name <> '' AND par_group_id <> 0 THEN

			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT 
				a.application_id, application_name, channel_type, record_count
				FROM
				(
					SELECT 
					application_id,
					IFNULL(SUM(incident_count), 0) AS record_count
				
					FROM rpt_dlp_oa_application_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND a.GroupId = par_group_id
					GROUP BY application_id
					ORDER BY record_count DESC LIMIT 5
				) AS a, rpt_dlp_application_master_dim AS b
				WHERE a.application_id = b.application_id
				ORDER BY record_count DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT 
				a.application_id, application_name, channel_type, record_count
				FROM
				(
					SELECT 
					application_id,
					IFNULL(SUM(incident_count), 0) AS record_count
				
					FROM rpt_dlp_oa_application_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND a.GroupId = par_group_id
					GROUP BY application_id
					ORDER BY record_count DESC LIMIT 5
				) AS a, rpt_dlp_application_master_dim AS b
				WHERE a.application_id = b.application_id
				ORDER BY record_count DESC;	
			END IF;
			
		ELSEIF par_client_name='' AND par_group_id<>0 THEN
		
			SELECT 
			a.application_id, application_name, channel_type, record_count
			FROM
			(
				SELECT 
				application_id,
				IFNULL(SUM(incident_count), 0) AS record_count
			
				FROM rpt_dlp_oa_application_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId = par_group_id
				GROUP BY application_id
				ORDER BY record_count DESC LIMIT 5
			) AS a, rpt_dlp_application_master_dim AS b
			WHERE a.application_id = b.application_id
			ORDER BY record_count DESC;
			
		ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
		
			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT 
				a.application_id, application_name, channel_type, record_count
				FROM
				(
					SELECT 
					application_id,
					IFNULL(SUM(incident_count), 0) AS record_count
				
					FROM rpt_dlp_oa_application_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					GROUP BY application_id
					ORDER BY record_count DESC LIMIT 5
				) AS a, rpt_dlp_application_master_dim AS b
				WHERE a.application_id = b.application_id
				ORDER BY record_count DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT 
				a.application_id, application_name, channel_type, record_count
				FROM
				(
					SELECT 
					application_id,
					IFNULL(SUM(incident_count), 0) AS record_count
				
					FROM rpt_dlp_oa_application_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str
					GROUP BY application_id
					ORDER BY record_count DESC LIMIT 5
				) AS a, rpt_dlp_application_master_dim AS b
				WHERE a.application_id = b.application_id
				ORDER BY record_count DESC;	
			END IF;
			
		ELSEIF par_client_name='' AND par_group_id = 0 THEN
		
			SELECT 
			a.application_id, application_name, channel_type, record_count
			FROM
			(
				SELECT 
				application_id,
				IFNULL(SUM(incident_count), 0) AS record_count
			
				FROM rpt_dlp_oa_application_summary AS a USE INDEX(idx_incident_date_only)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				GROUP BY application_id
				ORDER BY record_count DESC LIMIT 5
			) AS a, rpt_dlp_application_master_dim AS b
			WHERE a.application_id = b.application_id
			ORDER BY record_count DESC;
			
		END IF;

	ELSE
	
		SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
		
		IF par_client_name <> '' THEN

			SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
			FROM client_master 
			WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
			
			IF var_client_count > 1 THEN
				SELECT 
				a.application_id, application_name, channel_type, record_count
				FROM
				(
					SELECT 
					application_id,
					IFNULL(SUM(incident_count), 0) AS record_count
				
					FROM rpt_dlp_oa_application_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND a.user_id = var_user_id
					GROUP BY application_id
					ORDER BY record_count DESC LIMIT 5
				) AS a, rpt_dlp_application_master_dim AS b
				WHERE a.application_id = b.application_id
				ORDER BY record_count DESC;
			ELSEIF var_client_count = 1 THEN
				SELECT 
				a.application_id, application_name, channel_type, record_count
				FROM
				(
					SELECT 
					application_id,
					IFNULL(SUM(incident_count), 0) AS record_count
				
					FROM rpt_dlp_oa_application_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND a.user_id = var_user_id
					GROUP BY application_id
					ORDER BY record_count DESC LIMIT 5
				) AS a, rpt_dlp_application_master_dim AS b
				WHERE a.application_id = b.application_id
				ORDER BY record_count DESC;	
			END IF;
		
		ELSE
			
			SELECT 
				a.application_id, application_name, channel_type, record_count
				FROM
				(
					SELECT 
					application_id,
					IFNULL(SUM(incident_count), 0) AS record_count
				
					FROM rpt_dlp_oa_application_summary AS a USE INDEX(idx_incident_date_only, idx_user_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND a.user_id = var_user_id
					GROUP BY application_id
					ORDER BY record_count DESC LIMIT 5
				) AS a, rpt_dlp_application_master_dim AS b
				WHERE a.application_id = b.application_id
				ORDER BY record_count DESC;
				
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_oa_rpt_get_count_pr` ;
DELIMITER $$
CREATE PROCEDURE `dlp_oa_rpt_get_count_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_channel_type_id INT(2)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
	
		SELECT 
		IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
		FROM rpt_dlp_oa_summary;
	
	ELSE
		
		IF par_username = '' THEN
			IF par_channel_type_id <> 0 THEN
			
				IF par_client_name <> '' AND par_group_id <> 0 THEN
				
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND a.channel_type_id = par_channel_type_id;
					ELSEIF var_client_count = 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str 
						AND a.GroupId = par_group_id
						AND a.channel_type_id = par_channel_type_id;
					END IF;
					
				ELSEIF par_client_name='' AND par_group_id<>0 THEN
				
					SELECT 
					IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
			
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND a.channel_type_id = par_channel_type_id;
					
				ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.channel_type_id = par_channel_type_id;
					ELSEIF var_client_count = 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
						
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.channel_type_id = par_channel_type_id;
					END IF;
					
				ELSEIF par_client_name='' AND par_group_id = 0 THEN
				
					SELECT 
					IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
				
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.channel_type_id = par_channel_type_id;
				
				END IF;
				
			ELSE
				
				IF par_client_name <> '' AND par_group_id <> 0 THEN
			
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
					
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id;
					ELSEIF var_client_count = 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
					
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_GroupId)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str 
						AND a.GroupId = par_group_id;
					END IF;
					
				ELSEIF par_client_name='' AND par_group_id<>0 THEN
				
					SELECT 
					IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
				
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id;
					
				ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
					
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str);
					ELSEIF var_client_count = 1 THEN
						SELECT 
						IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
					
						FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id)
						WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str;
					END IF;
					
				ELSEIF par_client_name='' AND par_group_id = 0 THEN
				
					SELECT 
					IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
				
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
					
				END IF;
				
			END IF;
		
		ELSE
		
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
			
			IF par_client_name <> '' THEN
				
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
					
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str)
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND (par_channel_type_id = 0 OR a.channel_type_id = par_channel_type_id)
					AND a.user_id = var_user_id;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
					
					FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_client_id, idx_user_id, idx_GroupId)
					WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id = var_client_ids_str 
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND (par_channel_type_id = 0 OR a.channel_type_id = par_channel_type_id)
					AND a.user_id = var_user_id;
				END IF;
			
			ELSE 
			
				SELECT 
				IFNULL(SUM(removable_device_count + application_count + network_share_count + clipboard_count + print_screen_count + print_count), 0) AS record_count
				
				FROM rpt_dlp_oa_summary AS a USE INDEX(idx_incident_date_only, idx_user_id, idx_GroupId)
				WHERE a.incident_date_only >= unix_timestamp(par_start_date) AND a.incident_date_only <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id = 0 OR a.GroupId = par_group_id)
				AND (par_channel_type_id = 0 OR a.channel_type_id = par_channel_type_id)
				AND a.user_id = var_user_id;
			END IF;
			
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_oa_rpt_get_table_pr` ;
DELIMITER $$
CREATE PROCEDURE `dlp_oa_rpt_get_table_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_channel_id INT(11),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
	
		SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
		t1.client_id, incident_date_in_seconds, sender, receiver, subject
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
			channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
			FROM rpt_dlp_oa_fact AS a 
			ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
		) AS t1, client_master AS c, rpt_user_master AS d
		WHERE 
		t1.client_id=c.client_id
		AND t1.user_id = d.user_id
		ORDER BY incident_date DESC;
	
	ELSE
	
		IF par_username = '' THEN
			IF par_channel_id <> 0 THEN
				IF par_client_name<> '' AND par_group_id <> 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
						t1.client_id, incident_date_in_seconds, sender, receiver, subject
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
							channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
							FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND a.GroupId = par_group_id
							AND a.channel_type_id = par_channel_id
							ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
						) AS t1, client_master AS c, rpt_user_master AS d
						WHERE 
						t1.client_id=c.client_id
						AND t1.user_id = d.user_id
						ORDER BY incident_date DESC;
					ELSEIF var_client_count = 1 THEN
						SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
						t1.client_id, incident_date_in_seconds, sender, receiver, subject
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
							channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
							FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND a.GroupId = par_group_id
							AND a.channel_type_id = par_channel_id
							ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
						) AS t1, client_master AS c, rpt_user_master AS d
						WHERE 
						t1.client_id=c.client_id
						AND t1.user_id = d.user_id
						ORDER BY incident_date DESC;
					END IF;

				ELSEIF par_client_name='' AND par_group_id<>0 THEN
					SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
					t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.GroupId = par_group_id
						AND a.channel_type_id = par_channel_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c , rpt_user_master AS d
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;

				ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
						t1.client_id, incident_date_in_seconds, sender, receiver, subject
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
							channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
							FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND a.channel_type_id = par_channel_id
							ORDER BY a.incident_date DESC
							LIMIT par_start_index,par_max_records
						) AS t1, client_master AS c , rpt_user_master AS d
						WHERE 
						t1.client_id=c.client_id
						AND t1.user_id = d.user_id
						ORDER BY incident_date DESC;
					ELSEIF var_client_count = 1 THEN
						SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
						t1.client_id, incident_date_in_seconds, sender, receiver, subject
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
							channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
							FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND a.channel_type_id = par_channel_id
							ORDER BY a.incident_date DESC
							LIMIT par_start_index,par_max_records
						) AS t1, client_master AS c , rpt_user_master AS d
						WHERE 
						t1.client_id=c.client_id
						AND t1.user_id = d.user_id
						ORDER BY incident_date DESC;
					END IF;
				ELSEIF par_client_name='' AND par_group_id = 0 THEN
					SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
					t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a FORCE INDEX(idx_incident_date) 
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.channel_type_id = par_channel_id
						ORDER BY a.incident_date DESC
						LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c , rpt_user_master AS d
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_channel_id = 0 THEN
				IF par_client_name<> '' AND par_group_id <> 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
						t1.client_id, incident_date_in_seconds, sender, receiver, subject
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
							channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
							FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND a.GroupId = par_group_id
							ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
						) AS t1, client_master AS c, rpt_user_master AS d
						WHERE 
						t1.client_id=c.client_id
						AND t1.user_id = d.user_id
						ORDER BY incident_date DESC;
					ELSEIF var_client_count = 1 THEN
						SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
						t1.client_id, incident_date_in_seconds, sender, receiver, subject
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
							channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
							FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND a.GroupId = par_group_id
							ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
						) AS t1, client_master AS c, rpt_user_master AS d
						WHERE 
						t1.client_id=c.client_id
						AND t1.user_id = d.user_id
						ORDER BY incident_date DESC;
					END IF;

				ELSEIF par_client_name='' AND par_group_id<>0 THEN
					SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
					t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.GroupId = par_group_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c , rpt_user_master AS d
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;

				ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
					SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
					
					IF var_client_count > 1 THEN
						SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
						t1.client_id, incident_date_in_seconds, sender, receiver, subject
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
							channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
							FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							ORDER BY a.incident_date DESC
							LIMIT par_start_index,par_max_records
						) AS t1, client_master AS c , rpt_user_master AS d
						WHERE 
						t1.client_id=c.client_id
						AND t1.user_id = d.user_id
						ORDER BY incident_date DESC;
					ELSEIF var_client_count = 1 THEN
						SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
						t1.client_id, incident_date_in_seconds, sender, receiver, subject
						FROM
						(
							SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
							channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
							FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id)
							WHERE
							a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							ORDER BY a.incident_date DESC
							LIMIT par_start_index,par_max_records
						) AS t1, client_master AS c , rpt_user_master AS d
						WHERE 
						t1.client_id=c.client_id
						AND t1.user_id = d.user_id
						ORDER BY incident_date DESC;
					END IF;
				ELSEIF par_client_name='' AND par_group_id = 0 THEN
					SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
					t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a FORCE INDEX(idx_incident_date) 
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						ORDER BY a.incident_date DESC
						LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c , rpt_user_master AS d
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;
			END IF;
		
		ELSE
			
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
			
			IF par_client_name<> '' THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
					t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND (par_group_id = 0 OR a.GroupId = par_group_id)
						AND (par_channel_id = 0 OR a.channel_type_id = par_channel_id)
						AND a.user_id  = var_user_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c, rpt_user_master AS d
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
					t1.client_id, incident_date_in_seconds, sender, receiver, subject
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
						channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
						FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND (par_group_id = 0 OR a.GroupId = par_group_id)
						AND (par_channel_id = 0 OR a.channel_type_id = par_channel_id)
						AND a.user_id  = var_user_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS t1, client_master AS c, rpt_user_master AS d
					WHERE 
					t1.client_id=c.client_id
					AND t1.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;
			
			ELSE
			
				SELECT incident_date,computer_name,domain_name,d.username,str_ip_address,file_path,content_type,matched_item,channel_type_id,channel_type,channel_details,action_id,action_taken,
				t1.client_id, incident_date_in_seconds, sender, receiver, subject
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, file_path,content_type,matched_item,
					channel_type_id,channel_type,channel_details,action_id,action_taken, GroupId, user_id, incident_date AS incident_date_in_seconds, sender, receiver, subject
					FROM rpt_dlp_oa_fact AS a USE INDEX(idx_incident_date, idx_user_id, idx_GroupId)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND (par_channel_id = 0 OR a.channel_type_id = par_channel_id)
					AND a.user_id  = var_user_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS t1, client_master AS c, rpt_user_master AS d
				WHERE 
				t1.client_id=c.client_id
				AND t1.user_id = d.user_id
				ORDER BY incident_date DESC;
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_od_rpt_get_chart_details_table_pr` ;
DELIMITER $$
CREATE PROCEDURE `dlp_od_rpt_get_chart_details_table_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_matched_item_id INT(11),
	IN par_content_type_id INT(11),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT 
		incident_date, computer_name, d.username, str_ip_address, domain_name, file_type, file_path, incident_date_in_seconds
		FROM
		(
			SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, content_type_id, file_type, file_path, incident_date AS incident_date_in_seconds
			
			FROM rpt_dlp_od_fact AS a 
			ORDER BY a.incident_date DESC
		) AS a, client_master AS c, rpt_user_master AS d
		WHERE 
		a.client_id=c.client_id
		AND a.user_id = d.user_id
		ORDER BY incident_date DESC;
	ELSE
	
		IF par_username = '' THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					incident_date, computer_name, d.username, str_ip_address, domain_name, file_type, file_path, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, content_type_id, file_type, file_path, incident_date AS incident_date_in_seconds
						
						FROM rpt_dlp_od_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_content_type_id, idx_matched_item_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND a.content_type_id = par_content_type_id
						AND a.matched_item_id = par_matched_item_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					incident_date, computer_name, d.username, str_ip_address, domain_name, file_type, file_path, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, content_type_id, file_type, file_path, incident_date AS incident_date_in_seconds
						
						FROM rpt_dlp_od_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId, idx_content_type_id, idx_matched_item_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						AND a.content_type_id = par_content_type_id
						AND a.matched_item_id = par_matched_item_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
			
				SELECT 
				incident_date, computer_name, d.username, str_ip_address, domain_name, file_type, file_path, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, content_type_id, file_type, file_path, incident_date AS incident_date_in_seconds
					
					FROM rpt_dlp_od_fact AS a USE INDEX(idx_incident_date, idx_GroupId, idx_content_type_id, idx_matched_item_id)				
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND a.content_type_id = par_content_type_id
					AND a.matched_item_id = par_matched_item_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS c, rpt_user_master AS d
				WHERE 
				a.client_id=c.client_id
				AND a.user_id = d.user_id
				ORDER BY incident_date DESC;

			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					incident_date, computer_name, d.username, str_ip_address, domain_name, file_type, file_path, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, content_type_id, file_type, file_path, incident_date AS incident_date_in_seconds
						
						FROM rpt_dlp_od_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_content_type_id, idx_matched_item_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.content_type_id = par_content_type_id
						AND a.matched_item_id = par_matched_item_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					incident_date, computer_name, d.username, str_ip_address, domain_name, file_type, file_path, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, content_type_id, file_type, file_path, incident_date AS incident_date_in_seconds
						
						FROM rpt_dlp_od_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_content_type_id, idx_matched_item_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id=var_client_ids_str
						AND a.content_type_id = par_content_type_id
						AND a.matched_item_id = par_matched_item_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
			
				SELECT 
				incident_date, computer_name, d.username, str_ip_address, domain_name, file_type, file_path, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, content_type_id, file_type, file_path, incident_date AS incident_date_in_seconds
					
					FROM rpt_dlp_od_fact AS a USE INDEX(idx_incident_date, idx_content_type_id, idx_matched_item_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.content_type_id = par_content_type_id
					AND a.matched_item_id = par_matched_item_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS c, rpt_user_master AS d
				WHERE 
				a.client_id=c.client_id
				AND a.user_id = d.user_id
				ORDER BY incident_date DESC;
				
			END IF;
		
		ELSE
		
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
			
			IF par_client_name<> '' THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT 
					incident_date, computer_name, d.username, str_ip_address, domain_name, file_type, file_path, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, content_type_id, file_type, file_path, incident_date AS incident_date_in_seconds
						
						FROM rpt_dlp_od_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId, idx_content_type_id, idx_matched_item_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND (par_group_id = 0 OR a.GroupId = par_group_id)
						AND  a.user_id = var_user_id
						AND a.content_type_id = par_content_type_id
						AND a.matched_item_id = par_matched_item_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				ELSEIF var_client_count = 1 THEN
					SELECT 
					incident_date, computer_name, d.username, str_ip_address, domain_name, file_type, file_path, incident_date_in_seconds
					FROM
					(
						SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, content_type_id, file_type, file_path, incident_date AS incident_date_in_seconds
						
						FROM rpt_dlp_od_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId, idx_content_type_id, idx_matched_item_id)
						WHERE
						a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND (par_group_id = 0 OR a.GroupId = par_group_id)
						AND  a.user_id = var_user_id
						AND a.content_type_id = par_content_type_id
						AND a.matched_item_id = par_matched_item_id
						ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
					) AS a, client_master AS c, rpt_user_master AS d
					WHERE 
					a.client_id=c.client_id
					AND a.user_id = d.user_id
					ORDER BY incident_date DESC;
				END IF;
				
			ELSE
			
				SELECT 
				incident_date, computer_name, d.username, str_ip_address, domain_name, file_type, file_path, incident_date_in_seconds
				FROM
				(
					SELECT from_unixtime(incident_date) AS incident_date, client_id, user_id, content_type_id, file_type, file_path, incident_date AS incident_date_in_seconds
					
					FROM rpt_dlp_od_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_user_id, idx_GroupId, idx_content_type_id, idx_matched_item_id)
					WHERE
					a.incident_date >= unix_timestamp(par_start_date) AND a.incident_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (par_group_id = 0 OR a.GroupId = par_group_id)
					AND  a.user_id = var_user_id
					AND a.content_type_id = par_content_type_id
					AND a.matched_item_id = par_matched_item_id
					ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
				) AS a, client_master AS c, rpt_user_master AS d
				WHERE 
				a.client_id=c.client_id
				AND a.user_id = d.user_id
				ORDER BY incident_date DESC;
					
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_od_rpt_get_chart_pr` ;
DELIMITER $$
CREATE PROCEDURE `dlp_od_rpt_get_chart_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_content_type_id INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN -- To Export All Reports
		SELECT matched_item_id, scan_info_id, matched_item, SUM(incident_count) AS incident_count
		FROM rpt_dlp_od_chart_summary AS a
		GROUP BY matched_item_id ORDER BY incident_count DESC LIMIT 5;
	ELSE
		IF par_username = '' THEN
			IF par_client_name <> '' AND par_group_id <> 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT matched_item_id, scan_info_id, matched_item, SUM(incident_count) AS incident_count
					FROM rpt_dlp_od_chart_summary AS a
					WHERE a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) AND a.GroupId=par_group_id AND a.content_type_id=par_content_type_id
					GROUP BY matched_item_id ORDER BY incident_count DESC LIMIT 5;
				ELSEIF var_client_count = 1 THEN
					SELECT matched_item_id, scan_info_id, matched_item, SUM(incident_count) AS incident_count
					FROM rpt_dlp_od_chart_summary AS a
					WHERE a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id=var_client_ids_str AND a.GroupId=par_group_id AND a.content_type_id=par_content_type_id
					GROUP BY matched_item_id ORDER BY incident_count DESC LIMIT 5;
				END IF;
				
			ELSEIF par_client_name='' AND par_group_id<>0 THEN

				SELECT matched_item_id, scan_info_id, matched_item, SUM(incident_count) AS incident_count
				FROM rpt_dlp_od_chart_summary AS a
				WHERE a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.GroupId=par_group_id AND a.content_type_id=par_content_type_id
				GROUP BY matched_item_id ORDER BY incident_count DESC LIMIT 5;
				
			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT matched_item_id, scan_info_id, matched_item, SUM(incident_count) AS incident_count
					FROM rpt_dlp_od_chart_summary AS a
					WHERE a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) AND a.content_type_id=par_content_type_id
					GROUP BY matched_item_id ORDER BY incident_count DESC LIMIT 5;
				ELSEIF var_client_count = 1 THEN
					SELECT matched_item_id, scan_info_id, matched_item, SUM(incident_count) AS incident_count
					FROM rpt_dlp_od_chart_summary AS a
					WHERE a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id=var_client_ids_str AND a.content_type_id=par_content_type_id
					GROUP BY matched_item_id ORDER BY incident_count DESC LIMIT 5;
				END IF;
				
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				SELECT matched_item_id, scan_info_id, matched_item, SUM(incident_count) AS incident_count
				FROM rpt_dlp_od_chart_summary AS a
				WHERE a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND a.content_type_id=par_content_type_id
				GROUP BY matched_item_id ORDER BY incident_count DESC LIMIT 5;
			END IF;
		ELSE
		
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
			
			IF par_client_name <> '' THEN
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					SELECT matched_item_id, scan_info_id, matched_item, SUM(incident_count) AS incident_count
					FROM rpt_dlp_od_chart_summary AS a
					WHERE a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND FIND_IN_SET(a.client_id, var_client_ids_str) 
					AND (par_group_id = 0 OR a.GroupId=par_group_id )
					AND a.content_type_id=par_content_type_id
					AND a.user_id=var_user_id
					GROUP BY matched_item_id ORDER BY incident_count DESC LIMIT 5;
				ELSEIF var_client_count = 1 THEN
					SELECT matched_item_id, scan_info_id, matched_item, SUM(incident_count) AS incident_count
					FROM rpt_dlp_od_chart_summary AS a
					WHERE a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.client_id=var_client_ids_str 
					AND (par_group_id = 0 OR a.GroupId=par_group_id )
					AND a.content_type_id=par_content_type_id
					AND a.user_id=var_user_id
					GROUP BY matched_item_id ORDER BY incident_count DESC LIMIT 5;
				END IF;
				
			ELSE
			
				SELECT matched_item_id, scan_info_id, matched_item, SUM(incident_count) AS incident_count
				FROM rpt_dlp_od_chart_summary AS a
				WHERE a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
				AND (par_group_id = 0 OR a.GroupId=par_group_id )
				AND a.content_type_id=par_content_type_id
				AND a.user_id=var_user_id
				GROUP BY matched_item_id ORDER BY incident_count DESC LIMIT 5;
				
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_od_rpt_get_count_pr`;
DELIMITER $$
CREATE PROCEDURE `dlp_od_rpt_get_count_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
    IN par_client_name VARCHAR(45),
    IN par_username VARCHAR(512),
	In par_content_type_id INT(11)	
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN 
		IF par_content_type_id = 2 THEN
			SELECT IFNULL(COUNT(*), 0) AS record_count FROM rpt_dlp_od_summarized_fact where confident_data_inc_count <> 0;
        ELSEIF par_content_type_id = 3 THEN
			SELECT IFNULL(COUNT(*), 0) AS record_count FROM rpt_dlp_od_summarized_fact where data_dictionary_inc_count <> 0;
        ELSE
			SELECT IFNULL(COUNT(*), 0) AS record_count FROM rpt_dlp_od_summarized_fact;
        END IF;
	ELSE
		IF par_username = '' THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					IF par_content_type_id = 2 THEN
						SELECT IFNULL(COUNT(*), 0) AS record_count						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND confident_data_inc_count <> 0;
					ELSEIF par_content_type_id = 3 THEN
						SELECT IFNULL(COUNT(*), 0) AS record_count						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id
						AND data_dictionary_inc_count <> 0;
					ELSE
						SELECT IFNULL(COUNT(*), 0) AS record_count						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND a.GroupId = par_group_id;
					END IF;	
				ELSEIF var_client_count = 1 THEN
					IF par_content_type_id = 2 THEN
						SELECT IFNULL(COUNT(*), 0) AS record_count						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						AND confident_data_inc_count <> 0;
					ELSEIF par_content_type_id = 3 THEN
						SELECT IFNULL(COUNT(*), 0) AS record_count						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id
						AND data_dictionary_inc_count <> 0;
					ELSE
						SELECT IFNULL(COUNT(*), 0) AS record_count						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND a.GroupId = par_group_id;
					END IF;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				IF par_content_type_id = 2 THEN
					SELECT IFNULL(COUNT(*), 0) AS record_count						
					FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_GroupId)
					WHERE
					a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND confident_data_inc_count <> 0;
				ELSEIF par_content_type_id = 3 THEN
					SELECT IFNULL(COUNT(*), 0) AS record_count						
					FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_GroupId)
					WHERE
					a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id
					AND data_dictionary_inc_count <> 0;
				ELSE
					SELECT IFNULL(COUNT(*), 0) AS record_count						
					FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_GroupId)
					WHERE
					a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND a.GroupId = par_group_id;
				END IF;
			ELSEIF par_client_name <> '' AND par_group_id = 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					IF par_content_type_id = 2 THEN
						SELECT IFNULL(COUNT(*), 0) AS record_count					
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND confident_data_inc_count <> 0;
					ELSEIF par_content_type_id = 3 THEN
						SELECT IFNULL(COUNT(*), 0) AS record_count					
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND data_dictionary_inc_count <> 0;
					ELSE 
						SELECT IFNULL(COUNT(*), 0) AS record_count					
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str);
					END IF;
				ELSEIF var_client_count = 1 THEN
					IF par_content_type_id = 2 THEN
						SELECT IFNULL(COUNT(*), 0) AS record_count					
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND confident_data_inc_count <> 0;
					ELSEIF par_content_type_id = 3 THEN
						SELECT IFNULL(COUNT(*), 0) AS record_count					
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND data_dictionary_inc_count <> 0;
					ELSE
						SELECT IFNULL(COUNT(*), 0) AS record_count					
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str;
					END IF;	
				END IF;
			ELSEIF par_client_name='' AND par_group_id = 0 THEN
				IF par_content_type_id = 2 THEN
					SELECT IFNULL(COUNT(*), 0) AS record_count					
					FROM rpt_dlp_od_summarized_fact AS a FORCE INDEX(idx_scan_start_date) 
					WHERE
					a.scan_start_date >= unix_timestamp(par_start_date) AND confident_data_inc_count <> 0 AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
				ELSEIF par_content_type_id = 3 THEN
					SELECT IFNULL(COUNT(*), 0) AS record_count					
					FROM rpt_dlp_od_summarized_fact AS a FORCE INDEX(idx_scan_start_date) 
					WHERE
					a.scan_start_date >= unix_timestamp(par_start_date) AND data_dictionary_inc_count <> 0 AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
				ELSE
					SELECT IFNULL(COUNT(*), 0) AS record_count					
					FROM rpt_dlp_od_summarized_fact AS a FORCE INDEX(idx_scan_start_date) 
					WHERE
					a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second);
				END IF;
			END IF;
		ELSE
		
			SELECT user_id INTO var_user_id FROM rpt_user_master WHERE username = par_username LIMIT 1;
			
			IF par_client_name<> '' THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str 
				FROM client_master 
				WHERE (par_group_id = 0 OR GroupId=par_group_id) AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					IF par_content_type_id = 2 THEN
						SELECT IFNULL(COUNT(*), 0) AS record_count						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND (par_group_id = 0 OR GroupId=par_group_id)
						AND a.user_id = var_user_id
						AND confident_data_inc_count <> 0;
					ELSEIF par_content_type_id = 3 THEN
						SELECT IFNULL(COUNT(*), 0) AS record_count						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND (par_group_id = 0 OR GroupId=par_group_id)
						AND a.user_id = var_user_id
						AND data_dictionary_inc_count <> 0;
					ELSE
						SELECT IFNULL(COUNT(*), 0) AS record_count						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND FIND_IN_SET(a.client_id, var_client_ids_str)
						AND (par_group_id = 0 OR GroupId=par_group_id)
						AND a.user_id = var_user_id;
					END IF;	
				ELSEIF var_client_count = 1 THEN
					IF par_content_type_id = 2 THEN
						SELECT IFNULL(COUNT(*), 0) AS record_count						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND (par_group_id = 0 OR GroupId=par_group_id)
						AND a.user_id = var_user_id
						AND confident_data_inc_count <> 0;
					ELSEIF par_content_type_id = 3 THEN
						SELECT IFNULL(COUNT(*), 0) AS record_count						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND (par_group_id = 0 OR GroupId=par_group_id)
						AND a.user_id = var_user_id
						AND data_dictionary_inc_count <> 0;
					ELSE
						SELECT IFNULL(COUNT(*), 0) AS record_count						
						FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
						WHERE
						a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
						AND a.client_id = var_client_ids_str
						AND (par_group_id = 0 OR GroupId=par_group_id)
						AND a.user_id = var_user_id;
					END IF;
				END IF;
			ELSE
			
				IF par_content_type_id = 2 THEN
					SELECT IFNULL(COUNT(*), 0) AS record_count						
					FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
					WHERE
					a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (par_group_id = 0 OR GroupId=par_group_id)
					AND a.user_id = var_user_id
					AND confident_data_inc_count <> 0;
				ELSEIF par_content_type_id = 3 THEN
					SELECT IFNULL(COUNT(*), 0) AS record_count						
					FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
					WHERE
					a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (par_group_id = 0 OR GroupId=par_group_id)
					AND a.user_id = var_user_id
					AND data_dictionary_inc_count <> 0;
				ELSE
					SELECT IFNULL(COUNT(*), 0) AS record_count						
					FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_user_id, idx_GroupId)
					WHERE
					a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
					AND (par_group_id = 0 OR GroupId=par_group_id)
					AND a.user_id = var_user_id;
				END IF;	
			END IF;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_od_rpt_get_table_details_pr`;
DELIMITER $$
CREATE PROCEDURE `dlp_od_rpt_get_table_details_pr`(
	IN par_content_type_id INT(11),
	IN par_scan_info_id CHAR(38),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN

	SET time_zone = '+00:00';
	
	IF par_content_type_id = -1 AND par_scan_info_id = -1 THEN -- To Export All Reports
		SELECT incident_date, file_type, file_path, content_type, content_type, conf_data_cat, matched_item, 
		computer_name, domain_name, incident_date_in_seconds
		FROM 
		(
			SELECT from_unixtime(incident_date) AS incident_date, file_type, file_path, content_type, conf_data_cat, 
			matched_item, client_id, incident_date AS incident_date_in_seconds
			FROM rpt_dlp_od_fact AS a
			ORDER BY a.incident_date DESC
		) AS a, client_master AS b
		WHERE a.client_id = b.client_id
		ORDER BY a.incident_date DESc;
	ELSE
		IF par_content_type_id <> 0 THEN
			SELECT incident_date, file_type, file_path, content_type, content_type, conf_data_cat, matched_item, 
			computer_name, domain_name, incident_date_in_seconds
			FROM 
			(
				SELECT from_unixtime(incident_date) AS incident_date, file_type, file_path, content_type, conf_data_cat, 
				matched_item, client_id, incident_date AS incident_date_in_seconds
				FROM rpt_dlp_od_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
				WHERE
				a.content_type_id = par_content_type_id
				AND a.scan_info_id = par_scan_info_id
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, client_master AS b
			WHERE a.client_id = b.client_id
			ORDER BY a.incident_date DESc;
		ELSE
			SELECT incident_date, file_type, file_path, content_type, content_type, conf_data_cat, matched_item, 
			computer_name, domain_name, incident_date_in_seconds
			FROM 
			(
				SELECT from_unixtime(incident_date) AS incident_date, file_type, file_path, content_type, conf_data_cat, 
				matched_item, client_id, incident_date AS incident_date_in_seconds
				FROM rpt_dlp_od_fact AS a USE INDEX(idx_incident_date, idx_client_id, idx_GroupId)
				WHERE
				a.scan_info_id = par_scan_info_id
				ORDER BY a.incident_date DESC LIMIT par_start_index,par_max_records
			) AS a, client_master AS b
			WHERE a.client_id = b.client_id
			ORDER BY a.incident_date DESc;
		END IF;
	END IF;
END $$
DELIMITER ;

DROP PROCEDURE IF EXISTS `dlp_od_rpt_get_table_pr`;
DELIMITER $$
CREATE PROCEDURE `dlp_od_rpt_get_table_pr`(
	IN par_start_date DATE,
	IN par_end_date DATE,
	IN par_group_id INT(11),
	IN par_client_name VARCHAR(45),
	IN par_username VARCHAR(512),
	IN par_content_type_id INT(11),
	IN par_start_index INT(11),
	IN par_max_records INT(11)
)
BEGIN
	DECLARE var_client_count BIGINT(20) DEFAULT NULL;	
	DECLARE var_client_ids_str VARCHAR(2000) DEFAULT NULL;
	DECLARE var_user_id BIGINT(20) DEFAULT NULL;
	
	SET time_zone = '+00:00';
	IF (par_start_date IS NULL AND par_end_date IS NULL) OR (par_start_date='1970-01-01' AND par_end_date='1970-01-01') THEN 
		IF par_content_type_id = 2 THEN
			SELECT 
			a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
			a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type, 
				IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
				IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
				scan_start_date AS incident_date_in_seconds
				
				FROM rpt_dlp_od_summarized_fact AS a where a.confident_data_inc_count <> 0
				ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
			) AS a, client_master AS c, rpt_user_master AS d
			WHERE 
			a.client_id=c.client_id
			AND a.user_id = d.user_id
			ORDER BY scan_start_date DESC;
		ELSEIF par_content_type_id = 3 THEN
			SELECT 
			a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
			a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type, 
				IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
				IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
				scan_start_date AS incident_date_in_seconds
				
				FROM rpt_dlp_od_summarized_fact AS a where a.data_dictionary_inc_count <> 0
				ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
			) AS a, client_master AS c, rpt_user_master AS d
			WHERE 
			a.client_id=c.client_id
			AND a.user_id = d.user_id
			ORDER BY scan_start_date DESC;
		ELSE
			SELECT 
			a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
			a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
			FROM
			(
				SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type, 
				IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
				IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
				scan_start_date AS incident_date_in_seconds
				
				FROM rpt_dlp_od_summarized_fact AS a
				ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
			) AS a, client_master AS c, rpt_user_master AS d
			WHERE 
			a.client_id=c.client_id
			AND a.user_id = d.user_id
			ORDER BY scan_start_date DESC;
		END IF;
	
	ELSE
		IF par_username = '' THEN
			IF par_client_name<> '' AND par_group_id <> 0 THEN
			
				SELECT COUNT(*), GROUP_CONCAT(`client_id` SEPARATOR ',') INTO var_client_count, var_client_ids_str FROM client_master WHERE GroupId=par_group_id AND computer_name=par_client_name;
				
				IF var_client_count > 1 THEN
					IF par_content_type_id = 2 THEN
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_GroupId)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND a.GroupId = par_group_id
							AND a.confident_data_inc_count <> 0
							ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
						) AS a, client_master AS c, rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
					ELSEIF par_content_type_id = 3 THEN
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_GroupId)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND a.GroupId = par_group_id
							AND a.data_dictionary_inc_count <> 0
							ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
						) AS a, client_master AS c, rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
					ELSE
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_GroupId)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND FIND_IN_SET(a.client_id, var_client_ids_str)
							AND a.GroupId = par_group_id
							ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
						) AS a, client_master AS c, rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
					END IF;
				ELSEIF var_client_count = 1 THEN
					IF par_content_type_id = 2 THEN
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_GroupId)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND a.GroupId = par_group_id
							AND a.confident_data_inc_count <> 0
							ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
						) AS a, client_master AS c, rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
					ELSEIF par_content_type_id = 3 THEN
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_GroupId)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND a.GroupId = par_group_id
							AND a.data_dictionary_inc_count <> 0
							ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
						) AS a, client_master AS c, rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
					ELSE
						SELECT 
						a.scan_info_id, scan_start_date, computer_name, str_ip_address, domain_name, d.username, 
						a.scan_type, confident_data_inc_count, data_dictionary_inc_count, incident_date_in_seconds
						FROM
						(
							SELECT from_unixtime(scan_start_date) AS scan_start_date, client_id, scan_info_id, GroupId, user_id,scan_type,
							IFNULL(confident_data_inc_count, 0) AS confident_data_inc_count, 
							IFNULL(data_dictionary_inc_count, 0) AS data_dictionary_inc_count,
							scan_start_date AS incident_date_in_seconds
							
							FROM rpt_dlp_od_summarized_fact AS a USE INDEX(idx_scan_start_date, idx_client_id, idx_GroupId)
							WHERE
							a.scan_start_date >= unix_timestamp(par_start_date) AND a.scan_start_date <= unix_timestamp(par_end_date + interval 1 day - interval 1 second)
							AND a.client_id = var_client_ids_str
							AND a.GroupId = par_group_id
							ORDER BY a.scan_start_date DESC LIMIT par_start_index,par_max_records
						) AS a, client_master AS c, rpt_user_master AS d
						WHERE 
						a.client_id=c.client_id
						AND a.user_id = d.user_id
						ORDER BY scan_start_date DESC;
					END IF;
				END IF;

			ELSEIF par_client_name='' AND par_group_id<>0 THEN
				IF par_content_type_id = 2 THEN
