SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Absense Instances
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

gavinb80
Starting Member

United Kingdom
2 Posts

Posted - 11/15/2012 :  05:09:22  Show Profile  Reply with Quote
Morning all,

I have an issue and it is driving me crazy, so was helping someone may be able to help :-D

I have a list of people and a list of dates and leave types such as :

PERSON | DATE | SHIFTTYPE
---------------------------------------------------------
000001 | 2012-10-01 00:00 | WORKING
000001 | 2012-10-02 00:00 | SICK
000001 | 2012-10-03 00:00 | SICK
000001 | 2012-10-04 00:00 | DAYOFF
000001 | 2012-10-05 00:00 | SICK
000001 | 2012-10-06 00:00 | WORKING
000001 | 2012-10-07 00:00 | WORKING
000001 | 2012-10-08 00:00 | SICK
000001 | 2012-10-09 00:00 | SICK

From this data I need to calculate absence instances, so the first instance would be 2012-10-02 to 2012-10-05 and the second instance would be 2012-10-08 to 2012-10-09.

I am using SS2k8 R2, I really am at a loss with this one so any help would be greatly appreciated?

Many thanks

Gavin

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 11/15/2012 :  05:20:34  Show Profile  Reply with Quote

SELECT t.Person,MIN(t.[DATE]) AS LeaveStart,MAX(t.[Date]) AS LeaveEnd
FROM Table t
OUTER APPLY (SELECT MIN(DATE) AS [Date]
             FROM Table
             WHERE Person = t.Person
             AND SHIFTTYPE='WORKING'
             AND DATE > t.DATE
            )t1
WHERE t.SHIFTTYPE <> 'WORKING'
GROUP BY t.Person,t1.[Date]


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

Go to Top of Page

gavinb80
Starting Member

United Kingdom
2 Posts

Posted - 11/15/2012 :  07:11:22  Show Profile  Reply with Quote
Thanks visakh16, thats a great help
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 11/15/2012 :  10:08:37  Show Profile  Reply with Quote
welcome

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

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000