MySql Query Count_Working_Days

I have Emp table with following values


Emp_Id  Emp_Name    Subject        Dates
001      Smith      Java         07-02-2012
001      Smith      oracle       08-02-2012
001      smith      C++          10-02-2012
002      john       java         01-01-2012
002      john       SE           10-01-2012
002      john       c            10-01-2012
001      smith      physics      04-01-2012
001      smith      c#           07-02-2012
001      smith     javascript    07-02-2012

Now as we can see here smith studied only 3 days for month February and 1 for month Jan while john studied only 2 days for month January.

How can we calculate this count for any employee? As a e.g:Output should be in following way.


Emp_Id   Emp_Name  Month_Year   No_Of_Days_Studied_In_Month
001      smith      Feb12                 3
001      smith      Jan12                 1
002      john       Jan12                 2

What have you got so far?

SELECT Emp.Emp_Name,COUNT(Emp.date),DATE_FORMAT(Emp.date,‘%b%y’)AS Dates FROM Emp GROUP BY DATE_FORMAT(Emp.date,‘%b’),Emp.Emp_Name

I have tried this query, but its giving me output like this:



EMP_NAME	COUNT(EMP.DATE)	               DATES
smith    	      5	                        Feb12
john	              3	                        Jan12
smith	              1	                        Jan12

you want COUNT(DISTINCT emp.date), not COUNT(emp.date) :slight_smile:

It’s working!!!

Thnks…