试试这个查询
SELECT tblcontacts.ContactFullName AS Advisor,
COUNT( tblcases.CaseID ) AS Cases,
(COUNT( tblcases.CaseID )*100)/(SELECT COUNT( A.CaseID ) FROM tblcases AS A) as Percentage
FROM tblcases
INNER JOIN tblcontacts ON tblcontacts.ContactID = tblcases.ContactAssignedTo
GROUP BY tblcontacts.ContactFullName
考虑数据库架构和数据。
如果不存在则创建表tblcases
(
CaseID
int(11) NOT NULL AUTO_INCREMENT,
ContactAssignedTo
int(11) NOT NULL, PRIMARY KEY ( CaseID
)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=21;
插入tblcases
( CaseID
, ContactAssignedTo
) 值 (1, 1),(2, 1),(3, 1),(4, 1),(5, 2),(6, 2),(7, 3),(8 , 3),(9, 3),(10, 3),(11, 4),(12, 4),(13, 5),(14, 5),(15, 5),(16, 6 ),(17, 7),(18, 7),(19, 8),(20, 8);
如果不存在则创建表tblcontacts
(
ContactID
int(11) NOT NULL AUTO_INCREMENT,
ContactFullName
varchar(64) NOT NULL, PRIMARY KEY ( ContactID
)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7;
插入tblcontacts
( ContactID
, ContactFullName
) 值 (1, 'amit 1'),(2, 'amit 2'),(3, 'amit 3'),(4, 'amit 4'),(5, 'amit 5') ,(6, 'amit 6');
结果将是这样的,这对于数据库中的上述数据是正确的。
顾问案例百分比
阿米特 1 4 20.0000
阿米特 2 2 10.0000
阿米特 3 4 20.0000
阿米特 4 2 10.0000
阿米特 5 3 15.0000
阿米特 6 1 5.0000