Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Join tables where date between two dates in second table

I have a table that contains events and a second table that contains locations.  The events and location tables both contain a PersonID.  The events table also contains a datetime of the event.  The location table contains an arrive datetime and the depart datetime is derived based on the next location's datetime.  As a result, the last location for a person is null.  An event occurs at a location, but to determine the location of an event, it must be derived.  The location of the event will be: events.PersonID = location.PersonID and events.datetime between location.arrivedatetime and location.departdatetime.

The tables below show sample table data.

Events Table

PersonIDEventDateTime
1x8/1/2014 12:34:00 PM
1y8/1/2014 3:00:12 PM
1z8/1/2014 5:52:12 PM
2x8/1/2014 2:46:12 PM

Location Table

PersonIDLocationArriveDateTimeDepartDateTime
1Rm2008/1/2014 12:15:09 PM8/1/2014 5:09:34 PM
1Rm2018/1/2014 5:09:35 PM
2Rm1018/1/2014 2:09:14 PM

The desired end result is as below.

Events table

PersonIDEventDateTimeLocation
1x8/1/2014 12:34:00 PMRm200
1y8/1/2014 3:00:12 PMRm200
1z8/1/2014 5:52:12 PMRm201
2x8/1/2014 2:46:12 PMRm101

Any help in figuring this out is greatly appreciated.  Thanks!

1 Solution

Accepted Solutions
Not applicable
Author

Hi Robert,

Use this Script for loading Data

Location:

LOAD PersonID,

     Location,

     ArriveDateTime,

     DepartDateTime

FROM

[http://community.qlik.com/thread/130035]

(html, codepage is 1252, embedded labels, table is @2);

Event:

LOAD PersonID,

     Event,

     DateTime

FROM

[http://community.qlik.com/thread/130035]

(html, codepage is 1252, embedded labels, table is @1);

Inner Join

LOAD PersonID,

     Location,

     ArriveDateTime,

     DepartDateTime

FROM

[http://community.qlik.com/thread/130035]

(html, codepage is 1252, embedded labels, table is @2);

Inner Join

IntervalMatch(DateTime,PersonID) Load ArriveDateTime,DepartDateTime,PersonID Resident Location;

//DROP Table Event;

DROP Table Location;

Your Desired output will be like this

Capture.PNG.png

I am also attaching sample Qvw

Regards,

Vivek

View solution in original post

4 Replies
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

Take at look at "IntervalMatch" in the Ref Guide (Sections 27.3-4), specifically "Extended IntervalMatch". It should provide just what you are asking for.

Not applicable
Author

Hi Robert,

Use this Script for loading Data

Location:

LOAD PersonID,

     Location,

     ArriveDateTime,

     DepartDateTime

FROM

[http://community.qlik.com/thread/130035]

(html, codepage is 1252, embedded labels, table is @2);

Event:

LOAD PersonID,

     Event,

     DateTime

FROM

[http://community.qlik.com/thread/130035]

(html, codepage is 1252, embedded labels, table is @1);

Inner Join

LOAD PersonID,

     Location,

     ArriveDateTime,

     DepartDateTime

FROM

[http://community.qlik.com/thread/130035]

(html, codepage is 1252, embedded labels, table is @2);

Inner Join

IntervalMatch(DateTime,PersonID) Load ArriveDateTime,DepartDateTime,PersonID Resident Location;

//DROP Table Event;

DROP Table Location;

Your Desired output will be like this

Capture.PNG.png

I am also attaching sample Qvw

Regards,

Vivek

Not applicable
Author

Thank you both for the response.  Inervalmatch was exactly what was needed, but I was not able to get the desired results until I saw how the inner joins was used in Vivek's response.

Not applicable
Author

Hi,

Is it possible to do something similar to this but according to user selection?

For Ex if the user selects a date it gets one location, and if another date is selected it returns another value?

Basically doing this type of search at runtime?

Thanks

Jon