Question on SQL statement

LabKey Support Forum (Inactive)
Question on SQL statement Ben Bimber  2010-05-21 10:13
Status: Closed
 
I have a table with the columns: SubjectId, StartDate, EndDate, Location, Number of Roommates. I'd like to write a query that finds all animals without roomates, and reports their total days alone. The difficult part is that b/c locations can change, we could have multiple consecutive rows with zero roommates. take this data:

Id StartDate EndDate Location NumRoommates
Animal1 1/5/2000 <null> Room3 0
Animal1 1/3/2000 1/5/2000 Room2 0
Animal1 1/1/2000 1/3/2000 Room1 1
Animal1 12/1/1999 1/1/2000 Room0 0

For this animal, the current location has zero roomates. We can see he's been alone since 1/3/2000 and want to calculate 'days alone' using this date. Does anyone have suggestions on how to approach that using SQL?
 
 
Matthew Bellew responded:  2010-05-21 11:54
The tricky part is a selecting only the more recent row and handling the case where the animal has always been alone or has has a roommate at some point.

* Currently alone, look for the most recent record
  
  NumRoommates = 0 AND StartDate = MAX(StartDate)

* First record for this animal

  MIN(StartDate)

* Last Roommate

  MAX(EndDate) ... WHERE NumRoomates <> 0


putting it together, I think something like this (not tested)


SELECT
  RoommateInfo.Id,
  CASE WHEN LastRoommate IS NULL THEN FirstStart ELSE LastRoommate END AS AloneSince
  TIMESTAMPDIFF(SQL_TSI_DAY, NOW(), CASE WHEN LastRoommate IS NULL THEN FirstStart ELSE LastRoommate END) AS DaysAlone
FROM RoommateInfo
  INNER JOIN
    (SELECT Id, MIN(StartDate) AS FirstStart, MAX(StartDate) AS LastStart From RoommateInfo GROUP BY Id) Dates ON RoommateInfo.Id = Dates.Id
  LEFT OUTER JOIN
    (SELECT Id, MAX(EndDate) AS LastRoommate FROM RoommateInfo WHERE NumRoommates <> 0 GROUP BY Id) LastR ON RoommateInfo.Id = LastR.Id
WHERE
  RoommateInfo.NumRoommates = 0 AND StartDate = LastStartDate
 
Ben Bimber responded:  2010-05-25 15:05
thanks, that helped put me on the right track. i realized we should be basing this off study.animal instead of housing (8K records vs 300K), but the basic approach you outlined should work.