SELECT
u.`username` 'Name',
(SELECT
IFNULL(ROUND(COUNT(1) / COUNT(DISTINCT(DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y')))),0)
FROM
`leadfollwup_det` AS lf1
WHERE lf1.`DialedUserID` = u.userID
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') >= DATE_FORMAT(@FrDate,'%d-%b-%Y')
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') <= DATE_FORMAT(@ToDate,'%d-%b-%Y')
AND DATE_FORMAT( lf1.`DialedTime`, '%H') < 8) 'LessThan8AM',
(SELECT
IFNULL(ROUND(COUNT(1) / COUNT(DISTINCT(DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y')))),0)
FROM
`leadfollwup_det` AS lf1
WHERE lf1.`DialedUserID` = u.userID
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') >= DATE_FORMAT(@FrDate,'%d-%b-%Y')
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') <= DATE_FORMAT(@ToDate,'%d-%b-%Y')
AND DATE_FORMAT( lf1.`DialedTime`, '%H') = 8 ) 'From_8_To_9',
(SELECT
IFNULL(ROUND(COUNT(1) / COUNT(DISTINCT(DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y')))),0)
FROM
`leadfollwup_det` AS lf1
WHERE lf1.`DialedUserID` = u.userID
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') >= DATE_FORMAT(@FrDate,'%d-%b-%Y')
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') <= DATE_FORMAT(@ToDate,'%d-%b-%Y')
AND DATE_FORMAT( lf1.`DialedTime`, '%H') = 9 ) 'From_9_To_10',
(SELECT
IFNULL(ROUND(COUNT(1) / COUNT(DISTINCT(DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y')))),0)
FROM
`leadfollwup_det` AS lf1
WHERE lf1.`DialedUserID` = u.userID
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') >= DATE_FORMAT(@FrDate,'%d-%b-%Y')
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') <= DATE_FORMAT(@ToDate,'%d-%b-%Y')
AND DATE_FORMAT( lf1.`DialedTime`, '%H') = 10 ) 'From_10_To_11',
(SELECT
IFNULL(ROUND(COUNT(1) / COUNT(DISTINCT(DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y')))),0)
FROM
`leadfollwup_det` AS lf1
WHERE lf1.`DialedUserID` = u.userID
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') >= DATE_FORMAT(@FrDate,'%d-%b-%Y')
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') <= DATE_FORMAT(@ToDate,'%d-%b-%Y')
AND DATE_FORMAT( lf1.`DialedTime`, '%H') = 11 ) 'From_11_To_12',
(SELECT
IFNULL(ROUND(COUNT(1) / COUNT(DISTINCT(DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y')))),0)
FROM
`leadfollwup_det` AS lf1
WHERE lf1.`DialedUserID` = u.userID
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') >= DATE_FORMAT(@FrDate,'%d-%b-%Y')
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') <= DATE_FORMAT(@ToDate,'%d-%b-%Y')
AND DATE_FORMAT( lf1.`DialedTime`, '%H') = 12 ) 'From_12_To_1',
(SELECT
IFNULL(ROUND(COUNT(1) / COUNT(DISTINCT(DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y')))),0)
FROM
`leadfollwup_det` AS lf1
WHERE lf1.`DialedUserID` = u.userID
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') >= DATE_FORMAT(@FrDate,'%d-%b-%Y')
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') <= DATE_FORMAT(@ToDate,'%d-%b-%Y')
AND DATE_FORMAT( lf1.`DialedTime`, '%H') = 13 ) 'From_1_To_2',
(SELECT
IFNULL(ROUND(COUNT(1) / COUNT(DISTINCT(DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y')))),0)
FROM
`leadfollwup_det` AS lf1
WHERE lf1.`DialedUserID` = u.userID
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') >= DATE_FORMAT(@FrDate,'%d-%b-%Y')
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') <= DATE_FORMAT(@ToDate,'%d-%b-%Y')
AND DATE_FORMAT( lf1.`DialedTime`, '%H') = 14 ) 'From_2_To_3',
(SELECT
IFNULL(ROUND(COUNT(1) / COUNT(DISTINCT(DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y')))),0)
FROM
`leadfollwup_det` AS lf1
WHERE lf1.`DialedUserID` = u.userID
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') >= DATE_FORMAT(@FrDate,'%d-%b-%Y')
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') <= DATE_FORMAT(@ToDate,'%d-%b-%Y')
AND DATE_FORMAT( lf1.`DialedTime`, '%H') = 15 ) 'From_3_To_4',
(SELECT
IFNULL(ROUND(COUNT(1) / COUNT(DISTINCT(DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y')))),0)
FROM
`leadfollwup_det` AS lf1
WHERE lf1.`DialedUserID` = u.userID
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') >= DATE_FORMAT(@FrDate,'%d-%b-%Y')
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') <= DATE_FORMAT(@ToDate,'%d-%b-%Y')
AND DATE_FORMAT( lf1.`DialedTime`, '%H') = 16 ) 'From_4_To_5',
(SELECT
IFNULL(ROUND(COUNT(1) / COUNT(DISTINCT(DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y')))),0)
FROM
`leadfollwup_det` AS lf1
WHERE lf1.`DialedUserID` = u.userID
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') >= DATE_FORMAT(@FrDate,'%d-%b-%Y')
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') <= DATE_FORMAT(@ToDate,'%d-%b-%Y')
AND DATE_FORMAT( lf1.`DialedTime`, '%H') = 17 ) 'From_5_To_6',
(SELECT
IFNULL(ROUND(COUNT(1) / COUNT(DISTINCT(DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y')))),0)
FROM
`leadfollwup_det` AS lf1
WHERE lf1.`DialedUserID` = u.userID
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') >= DATE_FORMAT(@FrDate,'%d-%b-%Y')
AND DATE_FORMAT(lf1.`DialedTime`,'%d-%b-%Y') <= DATE_FORMAT(@ToDate,'%d-%b-%Y')
AND DATE_FORMAT( lf1.`DialedTime`, '%H') > 17 ) 'GreaterThan6PM'
FROM
`user_m` AS u
INNER JOIN `leadfollwup_det` AS lf
ON u.`userid` = lf.`DialedUserID`
AND DATE_FORMAT(lf.`DialedTime`,'%d-%b-%Y') >= DATE_FORMAT(@FrDate,'%d-%b-%Y')
AND DATE_FORMAT(lf.`DialedTime`,'%d-%b-%Y') <= DATE_FORMAT(@ToDate,'%d-%b-%Y')
# AND (DATE_FORMAT( lf.`DialedTime`, '%H') between 0 and 24 )
GROUP BY u.`userid` ;