| Author |
Topic |
|
rypi
Yak Posting Veteran
55 Posts |
Posted - 2012-08-22 : 20:56:30
|
Hello,I have a DB with the following tables and example data:DECLARE @Company TABLE([ID] [int] IDENTITY(1,1) NOT NULL,[CompanyName] [varchar](50) NOT NULL);DECLARE @Employee TABLE([ID] [int] IDENTITY(1,1) NOT NULL,[CompanyId] [int] NOT NULL,[Name] [varchar](25) NOT NULL);DECLARE @AbsenceType TABLE([ID] [int] IDENTITY(1,1) NOT NULL,[CompanyId] [int] NOT NULL,[AbsenceTypeDesc] [varchar](25) NOT NULL);DECLARE @AbsenceLedger TABLE([ID] [int] IDENTITY(1,1) NOT NULL,[EmployeeID] [int] NOT NULL,[AbsenceType] [int] NOT NULL,[StartDate] [date] NOT NULL,[EndDate] [date] NOT NULL);DECLARE @StatHolidays TABLE([ID] [int] IDENTITY(1,1) NOT NULL,[CompanyId] [int] NOT NULL,[StatDate] [date] NOT NULL,[StatDesc] [varchar](50) NOT NULL)INSERT INTO @Company (CompanyName) VALUES ('CompanyNameA');INSERT INTO @Company (CompanyName) VALUES ('CompanyNameB');INSERT INTO @EMPLOYEE (CompanyId, Name) VALUES (1, 'Steve');INSERT INTO @EMPLOYEE (CompanyId, Name) VALUES (1, 'Bill');INSERT INTO @EMPLOYEE (CompanyId, Name) VALUES (2, 'John');INSERT INTO @AbsenceType (CompanyId, AbsenceTypeDesc) VALUES (1, 'Vacation');INSERT INTO @AbsenceType (CompanyId, AbsenceTypeDesc) VALUES (1, 'FlexTime');INSERT INTO @AbsenceType (CompanyId, AbsenceTypeDesc) VALUES (2, 'Vacation');INSERT INTO @StatHolidays (CompanyId, StatDate, StatDesc) VALUES (1, '08-22-2012', 'Fake StatDay For Testing')INSERT INTO @AbsenceLedger (EmployeeID, AbsenceType, StartDate, EndDate) VALUES (1, 1, '08-20-2012', '08-28-2012')INSERT INTO @AbsenceLedger (EmployeeID, AbsenceType, StartDate, EndDate) VALUES (1, 2, '09-01-2012', '09-01-2012')INSERT INTO @AbsenceLedger (EmployeeID, AbsenceType, StartDate, EndDate) VALUES (2, 1, '08-17-2012', '08-20-2012')I need a query that will return a daily breakdown of an employees absence history.Using this example data, the query needs to return the data like so:Name Date AbsType=====================================Steve 08-20-2012 VacationSteve 08-21-2012 VacationSteve 08-23-2012 VacationSteve 08-24-2012 VacationSteve 08-27-2012 VacationSteve 08-28-2012 VacationSteve 09-01-2012 FlexTimeBill 08-17-2012 VacationBill 08-20-2012 Vacation If an entry in the AbsenceLedger overlaps with an entry in the StatHolidays table or a weekend (Sat and Sun) it is not included in the results.Thanks in advance, any help would be much appreciated. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-22 : 22:24:52
|
| [code];With AbsenceMatrixAS(SELECT EmployeeID,AbsenceType,StartDate AS DateVal,EndDateFROM @AbsenceLedgerUNION ALLSELECT EmployeeID,AbsenceType,DATEADD(dd,1,DateVal),EndDateFROM AbsenceMatrixWHERE DATEADD(dd,1,DateVal) <=EndDate)SELECT e.Name,am.DateVal,at.AbsenceTypeDescFROM @EMPLOYEE eINNER JOIN AbsenceMatrix amON am.EmployeeID = e.EmployeeIDINNER JOIN @AbsenceType atON at.AbsenceTypeID = am.AbsenceTypeWHERE am.DateVal NOT IN (SELECT StatDate FROM @StatHolidays)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rypi
Yak Posting Veteran
55 Posts |
Posted - 2012-08-23 : 00:42:57
|
| Thanks visakh16Any idea on how to filter out the weekends (sat and sun) too?Thanks again, appreciate the help! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-23 : 10:11:58
|
| [code];With AbsenceMatrixAS(SELECT EmployeeID,AbsenceType,StartDate AS DateVal,EndDateFROM @AbsenceLedgerUNION ALLSELECT EmployeeID,AbsenceType,DATEADD(dd,1,DateVal),EndDateFROM AbsenceMatrixWHERE DATEADD(dd,1,DateVal) <=EndDate)SELECT e.Name,am.DateVal,at.AbsenceTypeDescFROM @EMPLOYEE eINNER JOIN AbsenceMatrix amON am.EmployeeID = e.EmployeeIDINNER JOIN @AbsenceType atON at.AbsenceTypeID = am.AbsenceTypeWHERE am.DateVal NOT IN (SELECT StatDate FROM @StatHolidays)AND DATEDIFF(dd,0,am.DateVal) % 7 < 5[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|