Suppose that I have a table with three columns:
- EventID (PK)
- TagName
- TagValue
I need to create a query that results in something like:
- EventID
- TagName
- TagValue
- PreviousConditionTag
- PreviousConditionValue
Where PreviousConditionTag/Value is from TagName and TagValue of a previous row (when ordered by EventID).
In a simpler version of this problem, PreviousConditionTag was always the same as TagName – that is, I only needed to retrieve the previous value for the current TagName. I solved this using Oracle’s LAG analytic function, partitioning by the TagName.
However, I now need to perform something similar but for cases where PreviousConditionTag is an arbitrary tag related to TagName by another table where the relation between TagName and PreviousConditionTag is not one-to-one.
For example, if a given row has a TagName of “ABC”, the relation table may say that I need to look-up the previous value of either “IJK” or “XYZ”.
I was able to come up with this logic in an Oracle function that does a SELECT against the same table and looks for MAX(EventID) that matches the criteria. For example:
SELECT * FROM MyTable WHERE EventID = (
SELECT MAX(EventID) FROM MyTable WHERE TagName IN (
SELECT ConditionTagName FROM ConditionMappingTable WHERE TagName = [CurrentTagName]
)
) AND EventID <= [CurrentEventId]
However, as you can imagine, since this query is executed in a function for every row of MyTable, I am concerned about its performance.
I was trying to think of a way to use Oracle’s LAG analytic again, but I wasn’t sure how to come up with the PARTITION clause for it, since the partitions appear to overlap. (e.g. Tag ABC needs to look at IJK and XYZ, and Tag DEF needs to look at IJK and UVW)
Any ideas?
This is a rewritten form of the answer, now that I understand it better.
You want to look up overlapping sets of tags and still get the previous event id. The idea is the following:
Select the results where the Current and Condition tags are the same.
This may seem counterintuive, because you are looking things up backwards, by the condition rather than the current. And, you are multiplying the number of rows being processed. However, it may still be faster than the join solution you were using.