Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
See why IDC MarketScape names Qlik a 2025 Leader! Read more
cancel
Showing results for 
Search instead for 
Did you mean: 
beunderf
Partner - Creator II
Partner - Creator II

A Join with conditions

Hello everybody,

I have a question. I want to join data between tables using some conditions or rules.

I have two tables. One table gives me the schedule of a bus:

Table A:

CarIDStation sequenceDirectionStationDeparture time
1001SouthAmsterdam9:45:00
1002SouthUtrecht9:57:00
1003SouthVeenendaal10:14:00
1004SouthArnhem10:29:00
1005SouthNijmegen10:40:00
2001SouthAmsterdam9:55:00
2002SouthUtrecht10:07:00
2003SouthVeenendaal10:24:00
2004SouthArnhem10:39:00
2005SouthNijmegen10:50:00
3001NorthNijmegen9:45:00
3002NorthArnhem9:57:00
3003NorthVeenendaal10:14:00
3004NorthUtrecht10:29:00
3005NorthAmsterdam10:40:00

The other gives me chekin and checkout times and station of a passanger:

Table B:

TransactionIDcheckincheckoutcheckin stationchekout station
19:55:0010:31:00UtrechtArnhem
29:56:0010:31:00UtrechtArnhem
39:57:0010:33:00UtrechtArnhem
410:03:0010:42:00UtrechtArnhem
510:04:0010:43:00UtrechtArnhem
610:05:0010:45:00UtrechtArnhem

Now I want to join these to tables so that I now in which bus the passenger took. Expected result:

TransactionIDcheckincheckoutcheckin stationchekout stationUsed CarID
19:55:0010:31:00UtrechtArnhem100
210:05:0010:45:00UtrechtArnhem200
39:56:0010:31:00UtrechtArnhem100
49:57:0010:33:00UtrechtArnhem100
510:04:0010:43:00UtrechtArnhem200
610:03:0010:42:00UtrechtArnhem200

Extra info for the join:

Checkin time <= departure time

Checkout time => departure time of checkout station

The checkin station has a lower Station Sequence number as the Checkout station.

Does anyone know how to achieve this with Qlikview scripting. At this point, I have no idea, because I believe it not possible to compare fields of table A en Table B before or when the join is made? Who can help me?

I added a Qlikview file with the data

Best regards,

Frank

14 Replies
qliksus
Specialist II
Specialist II

Hi frank,

As per ur requirement for transactionid = 1 there will be 3 cardid matching the condition

100,200,300 but in ur expected result u need to see only 100

and incase of transaction id = 4 mactching records will be 200 and 300 but in ur expected result

u want to see only 200 so indirectly u are taking the minimum value after the result for each transaction id

if that is the case then see the attached file if i understood u wrongly then let me know

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

Frank,

Yes, I think Extended IntervalMatch may do the trick. The basic form

IntervalMatch(DepartureTime) LOAD checkin, checkout RESIDENT TableB;

will link up the cars with trips in TableB. You get a link whenever DepartureTIme is between checkin and checkout. But it does not consider the station, so will link too many cars. The extended form simply adds another key -- Station -- that should be included as part of the match criteria.

IntervalMatch(DepartureTime, Station) LOAD checkin, checkout, checkinStation RESIDENT TableB;

So you'll get link where the time range matches AND Station=checkinStation.

-Rob

http://robwunderlich.com

beunderf
Partner - Creator II
Partner - Creator II
Author

Rob,

Thanks a lot for your explanation. I understand now how it should work, but I don't get it to work. If  I use Test_A intervalmatch I get, as expected, a result with to many results, but it worked.

If I use extended intervalmatch Test_B, I get NO error, but als NO result. Do you have any idea what I am doing wrong. I added a qvw file. In the script on tab DATA, I scripted both ways.

Test_A:

IntervalMatch([Departure time]) LOAD checkin, checkout RESIDENT Transactions;

Test_B:

IntervalMatch([Departure time], Station) LOAD checkin, checkout, [checkin station] RESIDENT Transactions;

Your help is highly appreciated!!

Frank

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

You weren't matching beacuse I forgot to tell you that [checkin station] had to be called Station for the match.

There was still a problem where a  [Departure Time] matches too many trains, but I added some min logic following to get keep only the first match. Hopefully this will move you forward.

-Rob

INNER JOIN (Transactions)

IntervalMatch([Departure time], Station) LOAD checkin, checkout, [checkin station] as Station RESIDENT Transactions;

INNER JOIN (Transactions)

LOAD

          TransactionID,

          min([Departure time]) as [Departure time]

RESIDENT Transactions

GROUP BY TransactionID

;

beunderf
Partner - Creator II
Partner - Creator II
Author

Rob, this helps me a lot!

Thanks very much!