I have a table that records all events (events). Some of those events causes data to be written to a second table (returnreason) where we have a column that counts the iteration as the event may happen more than once. Unfortunately I cannot find a way to link the particular event to the iteration (1,2,3,etc.). I thought that if I could add an incremental column to the events, then I could use that to link to the iteration.
Event
FOLDERID EVENTID
123 7
123 12
123 17
ReturnReason
FOLDERID ITERATION REASONCODE
123 1 A
123 2 B
123 2 A
123 3 D
For instance, I need the query to tie eventid 12 to both iteration 2 results.
I have tried using LAG to compare the two rows to increase the value but I can't seem to get it to work. I am just trying to get the query to return the folderid and the number 1 for the first time it appears and the number 2 for the second time it appears and so on. I do not know if I can use that the give me the results I need in the end.
Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
Event
FOLDERID EVENTID
123 7
123 12
123 17
ReturnReason
FOLDERID ITERATION REASONCODE
123 1 A
123 2 B
123 2 A
123 3 D
For instance, I need the query to tie eventid 12 to both iteration 2 results.
I have tried using LAG to compare the two rows to increase the value but I can't seem to get it to work. I am just trying to get the query to return the folderid and the number 1 for the first time it appears and the number 2 for the second time it appears and so on. I do not know if I can use that the give me the results I need in the end.
Code:
select e.efolderid, e.eeventid,
case when lag(efolderid,1,0) over (order by efolderid) = e.efolderid then
lag(testCol,1,0) over (order by efolderid) + 1 else 1 end testCol
from eevent e
WHERE e.EACTIONNAME = 'Can Not Accept'
AND e.EFROMSTAGE = 'QA Non Acceptance Review'
Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook