Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7897787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:15:29+00:00 2026-06-03T08:15:29+00:00

I have a simple table that is used to record events against specific visits:

  • 0

I have a simple table that is used to record events against specific visits:

Describe Histories;
    +------------------+
    | Field            |
    +------------------+
    | HistoryId        |
    | VisitId          |
    | Location         |
    | Event            |
    | EventTime        |
    +------------------+

Individuals are associated with Visits (VisitId). For each Visit, an individual may have multiple History records. Events can be Admission, Transfer or Discharge.

I am trying to write a Query to calculate the duration in each Location for each individual. Note, that they may visit a Location multiple times for each Visit. An individual enters a Location with an Admission or Transfer Event and leaves with a Discharge or Transfer.

If an individual enters Location ‘A’, their Admission or Transfer record will list Location ‘A’, however if they transfer out their transfer out (or discharge) will list another Location, say ‘B’.

I therefore have to find the interval between the transfer into Location ‘A’ and the subsequent (in time) transfer to Location ‘B’. Intra location transfers are not evaluated.

I understand that the solution will probably be based on an INNER JOIN, however I am at a loss to understand how to select for the transfer “out” record that corresponds to the most recent transfer “in”.

I guess that this is reasonably complex – I hope I have been clear enough in my explanation.

Any guidance greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:15:30+00:00Added an answer on June 3, 2026 at 8:15 am

    Assuming a transfer or discharge is a unique event you could write like so

    SELECT
       b.EventTime - a.EventTime        
    FROM
       Histories a
       INNER JOIN Histories b
       ON a.VisitID = b.VisitID
    WHERE
       a.event = 'Admission'
       and
       b.event in ('Transfer', 'Discharge')
    

    If you were interested in the last transfer or discharge you would write

    SELECT
       b.EventTime - a.EventTime        
    FROM
       Histories a
       INNER JOIN  Histories b
        ON a.VisitID = b.VisitID
    
       INNER JOIN 
       (SELECT
             VisitId, 
             MAX(HistoryID) HistoryID
        FROM Histories 
        WHERE 
           b.event in ('Transfer', 'Discharge')
        GROUP BY 
           VisitId) maxHistory
       ON b.HistoryID = maxHistoryId.HistoryId
    
    WHERE
       a.event = 'Admission'
    

    However if a Visit can result in multiple visits as Andriy M mentions you have a Gaps And islands problem (specifically the islands)

    In that case you want the following

    SELECT  
           a.VisitId,
           a.Event a_Event, 
           a.Event b_Event, 
           a.EventTime a_EventTime,
           b.EventTime b_EventTime,
           b_EventTime - a_EventTime
    
    FROM   histories a 
           INNER JOIN histories B 
             ON a.visitID = b.visitID 
                AND a.EventTime < b.eventTime 
           INNER JOIN (SELECT a.VisitId, 
                              a.EventTime      a_EventTime, 
                              Min(b.EventTime) b_EventTime 
                       FROM   histories a 
                              INNER JOIN histories B 
                                ON a.visitID = b.visitID 
                                   AND a.EventTime < b.eventTime 
                       GROUP  BY a_EventTime, 
                                 a.VisitId) MinTime 
             ON a.VisitID = MinTime.VisitID 
                AND a.EventTime = a_EventTime 
                AND b.EventTime = b_EventTime 
    

    DEMO

    Using the following sample data

    CREATE TABLE Histories 
        (
         HistoryId int auto_increment primary key, 
         VisitId int,
         Location varchar(20),
         Event varchar(20), 
         EventTime datetime
        );
    
    INSERT INTO Histories
    (VisitId, Location, Event, EventTime)
    VALUES
    (1, 'A', 'Admission', '2012-01-01'),
    (1, 'A', 'Discharge', '2012-01-03'),
    (2, 'B', 'Admission', '2012-01-02'),
    (2, 'C', 'Transfer', '2012-01-05'),
    (2, 'C', 'Discharge', '2012-01-06'),
    (3, 'D', 'Admission', '2012-01-06'),
    (3, 'E', 'Transfer', '2012-01-07'),
    (3, 'F', 'Transfer', '2012-01-08'),
    (3, 'F', 'Discharge', '2012-01-10');
    

    You get the following results

    VISITID    A_EVENT   B_EVENT    A_EVENTTIME                     B_EVENTTIME                     B_EVENTTIME - A_EVENTTIME
    1          Admission Discharge  January, 01 2012 00:00:00-0800  January, 03 2012 00:00:00-0800  2000000
    2          Admission Transfer   January, 02 2012 00:00:00-0800  January, 05 2012 00:00:00-0800  3000000
    2          Transfer  Discharge  January, 05 2012 00:00:00-0800  January, 06 2012 00:00:00-0800  1000000
    3          Admission Transfer   January, 06 2012 00:00:00-0800  January, 07 2012 00:00:00-0800  1000000
    3          Transfer  Transfer   January, 07 2012 00:00:00-0800  January, 08 2012 00:00:00-0800  1000000
    3          Transfer  Discharge  January, 08 2012 00:00:00-0800  January, 10 2012 00:00:00-0800  2000000
    

    Notes:

    • This assumes you don’t care about Admissions/Transger that don’t have a corresponding discharge/transfer yet.
    • If you know that eventTime doesn’t change after the record is entered you could use historyID instead of eventime to determine the order of events.
    • You know how to get the Event Time difference in the format that you like
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple table (lets call it Table1) that has a NVARCHAR field
Say I have a simple table that contains username, firstname, lastname. How do I
I use SQL Server 2005. I have a simple logging table that my web
If I have a simple table where the data is such that the rows
I have two simple tables in my database. A card table that contains Id,
I have a simple cakephp app with table articles that has a cat_id column
I'm trying to have a simple html table, that highlights a row as a
I have a submission table that is very simple: userId, submissionGuid I want to
I have a simple procedure that selects 1000 rows from a table. For the
I have a simple map/reduce job that scans one hbase table, and modifies another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.