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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Need help writing SQL to get the following results

Author  Topic 

janteby
Starting Member

2 Posts

Posted - 2015-01-11 : 23:53:47
Hello

Lets say I have a Table, tblEmployees, with the following Fields

EmployeeID, LoginDate, Status

And this Data

EmployeeID LoginDate Status
-----------------------------
100 1/10/15 Open
100 1/09/15 Open
100 1/08/15 Open
100 1/07/15 Closed
100 1/06/15 Pending
100 1/05/15 Open
100 1/04/15 Open

I want to find the Date Difference between the most Recent Date Status=Open and going backwards the date the Status was last Open before it got changed to something else.

So with the example above the Date Difference between 1/10/15 and 1/8/15. I know the function to calculate the difference between 2 days, & can figure out the Most Recent Date (MAX(LogDate)) but can't figure out the other Date. If I was to use MIN(LogDate) then would return 1/4/15, but that is not correct as the Statuses changed in between. And of course would have to work for all Employees.

Any help with the SQL to do this would be greatly appreciated.

Thank you
Jeff


PhilG
Starting Member

3 Posts

Posted - 2015-01-12 : 05:53:43
SELECT TOP 2 [LoginDate] FROM [tblEmployees] ORDER BY [LoginDate] DESC
Will give you the highest and second highest date values.

If you want to return the difference in a single query though, you would need to do something more like this...
SELECT DATEDIFF(d, (SELECT MAX(E1.[LoginDate]) FROM [tblEmployees] E1 WHERE E1.[LoginDate] < (SELECT MAX(E2.[LoginDate]) FROM [tblEmployees] E2)),(SELECT MAX(E3.[LoginDate]) FROM [tblEmployees] E3))

Hope it helps
Phil
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2015-01-12 : 06:24:19
I would be inclined to look at interval packing.

Play with:

-- *** Consumable Test Data ***
-- Please provide in future
CREATE TABLE #t
(
EmployeeID int NOT NULL
,LoginDate date NOT NULL
,[status] varchar(10) NOT NULL
);
INSERT INTO #t
VALUES (100, '20150110', 'Open')
,(100, '20150109', 'Open')
,(100, '20150108', 'Open')
,(100, '20150107', 'Closed')
,(100, '20150106', 'Pending')
,(100, '20150105', 'Open')
,(100, '20150104', 'Open');
-- *** End Test Data ***

WITH Grps
AS
(
SELECT EmployeeID, LoginDate, [status]
,ROW_NUMBER() OVER (PARTITION BY EmployeeID ORDER BY LoginDate)
- ROW_NUMBER() OVER (PARTITION BY EmployeeID, [status] ORDER BY LoginDate) AS Grp
FROM #t
)
,GrpDates
AS
(
SELECT EmployeeID, [status], Grp
,MIN(LoginDate) AS StartDate, MAX(LoginDate) AS EndDate
FROM Grps
GROUP BY EmployeeID, [status], Grp
)
SELECT TOP 1 EmployeeID, [status], StartDate, EndDate
,DATEDIFF(day, StartDate, EndDate) AS DaysOpen
FROM GrpDates
WHERE [status] = 'open'
ORDER BY StartDate DESC;
Go to Top of Page

janteby
Starting Member

2 Posts

Posted - 2015-01-12 : 22:54:08
Hi PhilG thank you for taking the time to write but you misunderstood what I was looking for.

Ifor, aslo thank you for taking the time to write & that did it!
I never would have been able to do it as I never even heard of interval packing.

Thnx again
Jeff
Go to Top of Page
   

- Advertisement -