Skip to content
Advertisement

Get the first record from Table 2 where timestamp is greater than timestamp in Table 1

I have two tables.

EVENT_NARATIVE
EventNum
SEQ
Message
Message_Time
GRADE_HISTORY
EventNum
Grade
GradeChangeTime

The EVENT_NARATIVE Table has a lot of data (millions of rows) and each Event (EventNum) can have multiple EVENT_NARATIVE records.

The GRADE_HISTORY table contains thousands of rows and each EVENT can have multiple Grade changes.

In the EVENT_NARATIVE Table I need to find all records where the MESSAGE LIKE ‘CODE set to%’ There is only one MESSAGE that meets this criteria for each EVENT.

Once I have those records I need to find the first GRADE from the GRADE_HISTORY table that occurs after the Message_Time.

The result should look like

EventNum, Message, Message_Time, Grade, GradeChangeTime

My SQL looks like this but I know it doesn’t work

SQL is not my day job so any help is appreciated to get the result I need.

SAMPLE DATA EVENT_NARATIVE

SAMPLE DATA GRADE_HISTORY

Expected Result

Advertisement

Answer

Try this query

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement