You can use an aggregate function with a CASE
expression to pivot the data:
select studentenrollid,
count(case when studentstatus = '0' then studentid end) InActive,
count(case when studentstatus = '1' then studentid end) Active,
count(case when studentstatus = '2' then studentid end) Deleted
from yourtable
group by studentenrollid
See SQL Fiddle with Demo.
The result is:
| STUDENTENROLLID | INACTIVE | ACTIVE | DELETED |
-------------------------------------------------
| 3 | 0 | 2 | 1 |
| 7 | 0 | 1 | 0 |
| 8 | 0 | 1 | 0 |
| 9 | 1 | 0 | 0 |
Edit #1, using the sample data that you placed in the comments:
CREATE TABLE IF NOT EXISTS demo
(
studentid int(11) NOT NULL AUTO_INCREMENT,
studentenrollid int(11) NOT NULL,
studentstatus enum('0','1','2') NOT NULL DEFAULT '1',
PRIMARY KEY (studentid),
KEY studentenrollid (studentenrollid)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=84 ;
INSERT INTO demo (studentid, studentenrollid, studentstatus)
VALUES
(22, 41, '2'),
(23, 1,'1'),
(24, 2, '1'),
(25, 3, '1'),
(26, 41, '1'),
(29, 41, '1'),
(30, 41, '1'),
(31, 41, '1'),
(32, 41, '1'),
(33, 41, '1'),
(34, 41,'1'),
(35, 41, '1'),
(36, 41, '1'),
(37, 41, '1')
Your query would be:
select studentenrollid,
count(case when studentstatus = '0' then studentid end) InActive,
count(case when studentstatus = '1' then studentid end) Active,
count(case when studentstatus = '2' then studentid end) Deleted
from demo
group by studentenrollid
With the result (See SQL Fiddle with Demo):
| STUDENTENROLLID | INACTIVE | ACTIVE | DELETED |
-------------------------------------------------
| 1 | 0 | 1 | 0 |
| 2 | 0 | 1 | 0 |
| 3 | 0 | 1 | 0 |
| 41 | 0 | 10 | 1 |
Note, you are only getting a deleted count because that is the only value that you have in your data base (at least in the sample data).