| TimeStamp | USER ID | string_col |
|---|---|---|
| 1 | 100001 | Here |
| 2 | 100001 | there |
| 3 | 100001 | Apple |
| 4 | 200002 | this is |
| 5 | 200002 | that is |
| 6 | 200002 | Apple |
| 7 | 200002 | some |
| 8 | 200002 | summer |
| 9 | 200002 | winter |
| 10 | 200002 | Apple |
That is my raw table & I want to use the word "Apple" as a trigger word to concatenate ONLY N (For this example 2) rows above that for every userID.
Below table is the output I'm looking for:
| TimeStamp | USER ID | string_col | Result |
|---|---|---|---|
| 1 | 100001 | Here | null |
| 2 | 100001 | there | null |
| 3 | 100001 | Apple | Here There |
| 4 | 200002 | this is | null |
| 5 | 200002 | that is | null |
| 6 | 200002 | Apple | this is that is |
| 7 | 200002 | some | null |
| 8 | 200002 | summer | null |
| 9 | 200002 | winter | null |
| 10 | 200002 | Apple | summer winter |

For the passion of problem solving and coding, I would like to share this simple solution to see if it helps to answer your question:
The output looks like this:
