Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I Have 4 Request Status ID (0,1,2,3)

I have this table structure with data

C#
INSERT INTO `test` (`Requestid`, `RequestStatusID`) VALUES
(1,  '0'),
(2,  '0'),
(3,  '2'),
(4,  '2'),
(5,  '3');


I need to count the Counts for all RequestStatusID with this query:

SELECT COUNT(*) AS Counts,RequestStatusID FROM tblRequest GROUP BY RequestStatusID

this return following column
C#
   | Counts      |RequestStatusID
_________________________________
1  |  2          |      0
_________________________________
2  |  2          |      2
_________________________________
3  |  1          |      3



This works but the problem is that i need to display all results even if the count result is 0.
That means it does not display count result is 0.

The output should be

C#
   |Counts       |RequestStatusID
_________________________________
1  |  2          |      0
_________________________________
2  |  0          |      1
_________________________________
3  |  2          |      2
_________________________________
4  |  1          |      3


What I have tried:

I need to count the Counts for all RequestStatusID even if the count result is 0.
Posted
Updated 29-Apr-16 0:28am
Comments
Nigam,Ashish 29-Apr-16 3:13am    
where is requestStatusId 1 in your table? You are not inserting requestedStatusId 1 in your table. then How you will get 1 in RequestedStatusId?

1 solution

you can try this:

I have created 2 different tables:
 SELECT * FROM tblRequestStatus
id	RequestStatusID
1	  0         
2	  1         
3	  2         
4	  3         


 SELECT * FROM tblRequestID
Requestid	RequestStatusID
1	         0         
2	         0         
3	         2         
4	         2         
5	         3         


Then, use this query:
select COUNT(r.RequestStatusID) AS Counts,rs.RequestStatusID
from tblRequestStatus rs left join tblRequestID r
on r.RequestStatusID = rs.RequestStatusID
GROUP BY rs.RequestStatusID


to get the result:
Counts RequestStatusID
    2    0
    0    1
    2    2
    1    3
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900