cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • JMP 19 is here! See the new features at jmp.com/new.
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
Choose Language Hide Translation Bar
Onjai
Level IV

Selecting all rows in a data table where date/time is between two values in another table

Hi,

I have two data tables named, HVAC and Events.  I want to match date/times in HVAC based on start time and end times of the Events table and mark the matching rows in HVAC table with 1.

 

I have script that runs but does not populate the "Matched" column and I am not quite sure why.

 

Names Default to Here (1);
Clear Symbols();
// Reference both tables
timeEvents = DataTable("Events");
timeHVAC = DataTable("HVAC");

// Get column references
startCol = timeEvents:Round_when_started;
endCol = timeEvents:Round_when_ended;
timeHVACCol = timeHVAC:Round_date;
matchedCol = timeHVAC:Matched;
Batch Interactive( 1 );

// Loop through timeEvents and tag matches
For(i = 1, i <= N Rows(timeEvents), i++,
    startTime = startCol[i];
    endTime = endCol[i];

    // Loop through HVAC and tag matching rows
    For(j = 1, j <= N Rows(timeHVAC), j++,
        eventTime = timeHVACCol[j];
        If(eventTime >= startTime & eventTime <= endTime,
            matchedCol[j] = 1;
        );
    );
);
Batch Interactive( 0 );

// Notify user
New Window("Script Completed",
    Text Box("Lucy, I'm home!")
);
1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Selecting all rows in a data table where date/time is between two values in another table

Here is how I would handle this

Names Default To Here( 1 );
Clear Symbols();
// Reference both tables
timeEvents = Data Table( "Events" );
timeHVAC = Data Table( "HVAC" );

For( i = 1, i <= N Rows( timeEvents ), i++,
	Try(
		timeHVAC:Matched[timeHVAC << get rows where(
			timeHVAC:Round_date >= timeEvents:Round_when_started[i] & timeHVAC
			:Round_date <= timeEvents:Round_when_ended[i]
		)] = 1
	)
);

txnelson_0-1752856513298.png

 

Jim

View solution in original post

5 REPLIES 5
txnelson
Super User

Re: Selecting all rows in a data table where date/time is between two values in another table

Here is how I would handle this

Names Default To Here( 1 );
Clear Symbols();
// Reference both tables
timeEvents = Data Table( "Events" );
timeHVAC = Data Table( "HVAC" );

For( i = 1, i <= N Rows( timeEvents ), i++,
	Try(
		timeHVAC:Matched[timeHVAC << get rows where(
			timeHVAC:Round_date >= timeEvents:Round_when_started[i] & timeHVAC
			:Round_date <= timeEvents:Round_when_ended[i]
		)] = 1
	)
);

txnelson_0-1752856513298.png

 

Jim
Onjai
Level IV

Re: Selecting all rows in a data table where date/time is between two values in another table

Thank you Jim.

Worked!  

Cheers!

jthi
Super User

Re: Selecting all rows in a data table where date/time is between two values in another table

If this is how your data looks like (even hours) and the matches are defined like in your script, you should be able to do this with double join/update. Add Matched column to your Events table

jthi_3-1752857305704.png

 

and then first join using Round_date and Round_when_started

jthi_0-1752857206450.png

jthi_1-1752857238440.png

Then for second join use Round_date and round_when_ended.

jthi_2-1752857295210.png

And of course you can use something like Jim did demonstrate.

-Jarmo
Onjai
Level IV

Re: Selecting all rows in a data table where date/time is between two values in another table

Hi Jarmo,

Sorry I should have posted that I am using JMP and not JMP Pro.

It never crossed my mind to double join and may try that in Query Builder.

Cheers! 

jthi
Super User

Re: Selecting all rows in a data table where date/time is between two values in another table

I think you shouldn't need JMP Pro for what I did. I just used Tables > Update (should be available with standard JMP) twice but you could also do it with a single query using Query Builder

jthi_0-1752865548113.png

I added one calculated column

jthi_1-1752865580376.png

View more...

SQL query is

SELECT t1.Round_date, t1."Building pressure", t1."Plant Temp", t1."Zone Humdity", 
	t1."outside air temp", t3.Round_when_ended IS NOT NULL  OR  t2.Round_when_started IS NOT NULL AS Calc1, t3.Round_when_ended IS NOT NULL  OR  t2.Round_when_started IS NOT NULL AS "Calc1 2" 
FROM HVAC  t1 
	LEFT OUTER JOIN Events t3 
		ON  ( t1.Round_date = t3.Round_when_ended )  
	LEFT OUTER JOIN Events t2 
		ON  ( t1.Round_date = t2.Round_when_started );

And JMP's query builder script is something like this

New SQL Query(
	Version(130),
	Connection("JMP"),
	JMP Tables(["Events" => "\C:\Users\jarmo\Downloads\Events.jmp", "HVAC" => "\C:\Users\jarmo\Downloads\HVAC.jmp"]),
	QueryName("SQLQuery1"),
	Select(
		Column("Round_date", "t1", Numeric Format("m/d/y h:m:s", "0", "NO", "")),
		Column("Building pressure", "t1", Numeric Format("Fixed Dec", "0", "NO", "")),
		Column("Plant Temp", "t1", Numeric Format("Fixed Dec", "0", "NO", "")),
		Column("Zone Humdity", "t1", Numeric Format("Fixed Dec", "0", "NO", "")),
		Column("outside air temp", "t1", Numeric Format("Fixed Dec", "0", "NO", "")),
		ExprLookup("Calc1", JMP Name("Calc1"))
	),
	From(
		Table("HVAC", Alias("t1")),
		Table(
			"Events",
			Alias("t3"),
			Join(
				Type(Left Outer),
				EQ(
					Column("Round_date", "t1", Numeric Format("m/d/y h:m:s", "0", "NO", "")),
					Column("Round_when_ended", "t3", Numeric Format("y/m/d h:m:s", "0", "NO", ""))
				)
			)
		),
		Table(
			"Events",
			Alias("t2"),
			Join(
				Type(Left Outer),
				EQ(
					Column("Round_date", "t1", Numeric Format("m/d/y h:m:s", "0", "NO", "")),
					Column("Round_when_started", "t2", Numeric Format("y/m/d h:m:s", "0", "NO", ""))
				)
			)
		),
		Expression("t3.Round_when_ended IS NOT NULL  OR  t2.Round_when_started IS NOT NULL", Alias("Calc1"))
	),
	Where(Custom("", UI(Custom(Base("Continuous")))))
) << Run;

 

-Jarmo

Recommended Articles