'Getting a list of subsequence values in SQL

I have the following scenario and I am not sure if it is possible to implement it using SQL.

UID PID Event Timestamp
323 1 A 10:01
323 1 B 10:02
323 1 C 10:03
323 1 D 10:04
321 3 A 10:03
321 3 B 10:04
321 3 C 10:05

I need to get the subsequence of events following the event 'B' so the result will be something like :

UID PID list of events
323 1 C,D
321 3 C

Thank you in advance for your help



Solution 1:[1]

I know how to do this for MySQL and have a fiddler example here: http://sqlfiddle.com/#!9/ee6809/12

SELECT
   UID, a.PID, GROUP_CONCAT(EVENT)
FROM 
TableName AS a
LEFT Join (SELECT PID, eventtime FROM TableName WHERE event = 'B') b on a.PID = b.PID
WHERE a.eventtime > b.eventtime
GROUP BY UID, PID

Which would be the general concept for me. But redshift doesn't have GROUP_CONCAT so looking at this other post I think you can use listagg Group Concat in Redshift

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1