Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to write query to getting count of values (including nuu values) using group by...

see below example.

Emp_Id EmpName Marks
101 A 90
101 A 85
101 NULL 89
102 B 90
102 B 85
102 B 89
102 NULL 89
103 C 90
103 C 85
103 C 89
103 C 89
103 NULL 90

i want to get the count of 101,102,103.

for example, 101 -> 3 (including null)

102 -> 4 (inluding null)

103 -> 5 (including null)

How to write count query using group by
Posted

1 solution

SELECT EMP_ID,COUNT(EMP_ID) FROM EMP GROUP BY EMP_ID;

This will show the emp_id and the no of records present for that emp_id.
 
Share this answer
 
Comments
gani7787 3-Apr-12 6:31am    
no..it's not working.

it will come as below.

Emp_id Name Tot Count
101 NULL 1
101 A 2
102 NULL 1
102 B 3
103 NULL 1
103 C 4

But, i want 101 --> 2+1 = 3

102 --> 3+1 = 4

103 --> 4+1 = 5

like below o/p..

Emp_id Name Tot Count
101 A 3
102 B 4
103 C 5
bhagirathimfs 3-Apr-12 8:07am    
SELECT EMP_ID,EMP_NAME AS NAME,COUNT(EMP_ID) AS TOTAL FROM EMP GROUP BY EMP_ID;

If u write the above code than it will show
emp_id name total
101 A 3
102 B 4
103 C 5

If u write
SELECT EMP_ID,EMP_NAME AS NAME,COUNT(EMP_ID) AS TOTAL FROM EMP GROUP BY EMP_ID,NAME;

than this will show
emp_id name total
101 A 2
101 null 1
102 B 3
102 null 1
103 C 4
103 null 1

So please group by only emp_id

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