Splitting a table based on a column value

Hey, everybody!

I've got the following table that's getting it's data from a Named Query:


I'd like to split the table by the station value and create a table for each distinct station. Is there a way to do this?

Yes, see my Integration Toolkit module's groupBy expression function.

Thank you for answering. Unfortunately, I'm not able to add modules to the project I'm working on right now.

You'll have to script it, then.

The general approach would be to have a dictionary of station names, each holding a DatasetBuilder. As you iterate through the source, you'd retrieve the existing dataset builder for that station value, creating a new dataset builder as needed, and adding the source row to that builder.

After that, you'd loop through the key-value pairs of the dictionary, using the .build() method to get the finished inner dataset per station.

Thank you, I'll give this a try.

Sorry to bother you again, but do you have an example of this? I'm pretty new to this kind of programming coming from a PLC background and I'm havin difficulty getting this to work.

So, I just did one of these to break a historian "Tall" result dataset containing multiple tags mixed together into a dictionary (keyed by tag path) of separated datasets:

def tallHistoryByPath(ds):
	builderMap = {}
	for row in system.dataset.toPyDataSet(ds):
		p, v, ts = [row[c] for c in ['path', 'value', 'timestamp']]
		dsb = builderMap.get(p)
		if dsb is None:
			dsb = builderMap.setdefault(p, DatasetBuilder().colNames(['t_stamp', p]).colTypes([Date, Double]))
		dsb.addRow([ts, v])
	return [(p, dsb.build()) for (p, dsb) in builderMap.items()]

You can rework that to split by station.

Be sure to import the DatasetBuilder and the column types you need at the top of the library script where you put this.

3 Likes

Thanks, this was it! I was able to rework it and implement it into my application, really appreciate it.

1 Like

I should mention that the change to returning tuples of tagpath and dataset is to make the return value Perspective-friendly. Returning a dictionary with tag paths as keys is handy for a number of algorithms, but tag paths are not valid JSON keys. :man_shrugging:

I can see that, I ended up just taking the tables for my application, but it's a very clever script.