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
 Need Help with Date Range in Query

Author  Topic 

sqlnoob35
Starting Member

10 Posts

Posted - 2011-01-13 : 11:55:08
I am trying to create a query that will show me employees who had service between 2010-07-01 and 2010-09-01. The twist is I only need employees who started work between these days.

If I just use a simple BETWEEN command, it will show all employees since everyone had service in this range.


I know MAX and MIN functions are the key here, but I can't remember the syntax. I want the query to be

min(date) >= '2010-07-01' and max(date) <= '2010-09'01.


I wrote this query:


select distinct mbr_ssn_nbr from history
group by mbr_ssn_nbr
having min(MBR_HIST_SVC_CR_DT) >= '2010-07-01'

and exists (select distinct mbr_ssn_nbr from history
group by mbr_ssn_nbr
having max(MBR_HIST_SVC_CR_DT) <= '2010-09-01')




Unfortunately, it is showing people with credit dates occuring after 09-01-2010 so the logic fails for some reason. Can anyone help??

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-01-13 : 12:04:30
is table having single record per employee?

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

Go to Top of Page

sqlnoob35
Starting Member

10 Posts

Posted - 2011-01-13 : 12:20:30
No, there are multiple records for each employee
Go to Top of Page

JimL
SQL Slinging Yak Ranger

1537 Posts

Posted - 2011-01-13 : 12:26:46
Select Min(activitydate) as Startdate, EmployeeID
into #SD
from yourtable
Group by EmployeeID

Select EmployeeID
from dbo.#SD
where Startdate between YourStartDate and YourEndDate

Jim
Users <> Logic
Go to Top of Page

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2011-01-14 : 00:45:16
do you have any column like 'date of joining'
Go to Top of Page
   

- Advertisement -