Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting months based on number of hours in month

Status
Not open for further replies.
Joined
Dec 11, 2009
Messages
60
Location
US
I have the below code:

select p.employeeid, sum (actualunits) as actualunits
from ss_hcsscsa.dbo.prchecktc t
inner join ss_hcsscsa.dbo.prcheckheader p (NOLOCK) on p.checkhistid = t.checkhistid
where t.paycodeid in (1,2,3,4,5,6,7) and checkdate between '2009-01-01' and '2009-12-31'
group by employeeid

Now what I need to do is list how many of the months out of the year had 1000 actualunits: count distinct months where sum (actualunits) = 1000

Any suggestions?

Thanks!!
 
Not sure if I understand you.
Could you please provide some example data and desired result from it?

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
1. What is your SQL Server version.

2. You can use Datapart function to find out the month of the CheckDate or you can simply use Month(CheckDate)

e.g.
Code:
select p.employeeid, Month(checkDate) as [Month]
sum (actualunits) as actualunits    from ss_hcsscsa.dbo.prchecktc t     inner join ss_hcsscsa.dbo.prcheckheader p (NOLOCK) on p.checkhistid = t.checkhistid    
where t.paycodeid in (1,2,3,4,5,6,7) and checkdate between '2009-01-01' and '2009-12-31' 
   group by p.employeeid, Month(checkDate)
having sum(actualunits) = 1000

PluralSight Learning Library
 
I'm using SQL Server 2005


Sample data is:

Name TotalHours
Joe Smith 1,288
Don King 650
James Doe 875

End result:

Name TotalHours MonthsWorked
Joe Smith 1,288 8
Don King 650 5
James Doe 875 7

I need to know how many months out of the year they worked?

 
Maybe better:
Code:
....
count(DISTINCT Month(CheckDate)) as MonthsWorked 
...

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top