Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query with 2 Count criteria

Author  Topic 

markie88
Starting Member

3 Posts

Posted - 2012-01-13 : 21:30:30
Hi all,

I need help constructing a query that returns the number of days an employee works in May as well as the number of days they work in June in two separate columns for comparison.

I can easily get one to show the number of days in May and June in 2 separate queries, but need help getting into one.

I have this for the days in may:

Select name, Count(*) as Days_in_May
From tblHours, tblEmp
Where tblhours.empId=tblEmp.empID AND date >='2010-05-01' AND date <='2010-05-31'
Group By name

This gives

Name Days_in_May
Bob 18
Larry 22

and so on...

Any help is appreciated!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-14 : 00:16:39
[code]
Select name,DATEPART(mm,dateadd(mm,datediff(mm,0,[date]),0)) as monthval, Count(*) as Days_in_May
From tblHours, tblEmp
Where tblhours.empId=tblEmp.empID
Group By name,DATEPART(mm,dateadd(mm,datediff(mm,0,[date]),0))
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -