You can do it with GROUP_CONCAT()
window function which supports the ORDER BY
clause so you will have the scope
s in inbetween_pages
in the correct order, instead of GROUP_CONCAT()
aggregate function which does not support the ORDER BY
clause and the results that it returns are not guaranteed to be in a specific order:
SELECT DISTINCT id, first_page, last_page,
GROUP_CONCAT(CASE WHEN timestamp NOT IN (min_timestamp, max_timestamp) THEN scope END)
OVER (PARTITION BY id ORDER BY timestamp ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) inbetween_pages
FROM (
SELECT *,
FIRST_VALUE(scope) OVER (PARTITION BY id ORDER BY timestamp) first_page,
FIRST_VALUE(scope) OVER (PARTITION BY id ORDER BY timestamp DESC) last_page,
MIN(timestamp) OVER (PARTITION BY id) min_timestamp,
MAX(timestamp) OVER (PARTITION BY id) max_timestamp
FROM tablename
)
See the demo.
Results:
id |
first_page |
last_page |
inbetween_pages |
1 |
enter_page |
checkout |
view_product,checkout,search_page |
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…