Postgresql 9.2 DB which automatically collects data from various machines.
The DB stores all the data including the machine id, the firmware, the manufacturer id etc as well as the actual result data. In one stored field (varchar) there are 5 sub fields which are separated by the ^ character.
ACT18!!!8246-EN-2.00013151!1^7.00^F5260046959^H1P1O1R1C1Q1L1^1 (Machine 1)
The order of this data seems to vary from one machine to another. Eg machine 1 2 and 3. The string above shows the firmware version, in this case “7.0” and it appears in sub-field 2. However, another machine sends the data in a different sub-field – in this case sub-field 3 and the value is “1”
BACT/ALERT^A.00^1^^ (Machine 2)
I want to store the values “7.0” and “1” in a different field in a separate table using a CREATE TRIGGER t_machine_id AFTER INSERT function where I can choose which sub-field is used depending on the machine the data has come from.
Is split_part the best function to do this? Can anyone supply an example code that will do this? I can’t find anything in the documentation.
You need to (a) split the data using something like
regexp_split_to_tablethen (b) match which parts are which using some criteria, since you don’t have field position-order to rely on. Right now I don’t see any reliable rule to decide what’s the firmware version and what’s the machine number; you can’t really saywhere field <> machine_numberbecause if machine 1 had firmware version1you’d get no results.Given dummy data:
Something like:
will give you a table of split data elements with machine number, but then you need to decide which fields are which:
You may find the output of substituting
regexp_split_to_arraymore useful, depending on whether you can get any useful info from field order and how you intend to process the data.Say there are two firmware versions; version 1 sends
code^blah^fwvers^^and version 2 and higher sendscode^fwvers^blah^blah2^machineno. You can then differentiate between the two because you know that version 1 leaves the last two fields blank:results:
Of course, you’ve only provided two sample data, so the real matching rules are likely to be more complex. Consider writing an SQL function to extract the desired field(s) and return them from the array passed.