Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8003213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:35:53+00:00 2026-06-04T16:35:53+00:00

Suppose that I have a table with three columns: EventID (PK) TagName TagValue I

  • 0

Suppose that I have a table with three columns:

  1. EventID (PK)
  2. TagName
  3. TagValue

I need to create a query that results in something like:

  1. EventID
  2. TagName
  3. TagValue
  4. PreviousConditionTag
  5. 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:35:54+00:00Added an answer on June 4, 2026 at 4:35 pm

    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:

    1. Add to the mapping table the identity for all Current Tags (so Current Tag = Condition Tag)
    2. Join in the mapping table based on the condition tag, to get the current tags that match. So, the rows are getting relabeled with the “current” tag they match, and you can use this for the lag.
    3. Get the most recent EventId based on the lag logic, partitioning by the Current Tag.
    4. Select the results where the Current and Condition tags are the same.

      select t.*
      from (select t.*, mt.CurrentTagName, mt.ConditionTagName,
               lag(EventId, 1, NULL)
               over (partition by mt.CurrentTagName
                     order by EventId)
        from t join
             (select CurrentTagName, ConditionTagName
              from ((select CurrentTagName, ConditionTagName
                     from ConditionMappingTable mt
                    ) union all
                    (select distinct CurrentTagName, CurrentTagName
                     from ConditionMappingTable mt
                    )
                   ) mt
             )
             on mt.ConditionTagName = t.tagname
       ) t
      on CurrentTagName = ConditionTagName
      

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two columns in a table that represents a graph, the first
Suppose I have a table called 'test', into which there are three columns named
Suppose I have a table that looks like the following id | location |
Lets suppose that I have a Category table with a column that holds the
Suppose I have table A with a field that can be either 1 or
Suppose I have an array that mimics a database table. Each array element represents
Suppose I have employee and department table, employee has foreign key departmentID that is
Suppose that I have those three files: a.h //a.h header #include <stdio.h> int int_variable;
Suppose I have a table abc with columns p_id , u_id , and comments
Suppose I have a table amountInfo with columns (id, amount1, amount2, amount3) where amountX

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.