While aggregating an array, I need to remove empty strings and then combine all adjacent identical values. E.g.:
["","product","product","","product","","product","product","","product","product","","product","","","collection","product","","","product","product","","collection","order","checkout",""]
Should become:
["product","collection","product","collection","order","checkout"]
I have a working query with 4 nested selects:
SELECT array_agg( page_type_unique_pre) FILTER (WHERE page_type_unique_pre != '')
OVER (ORDER BY event_time) AS page_type_journey_unique
FROM (
SELECT CASE WHEN lag(last_page_type) OVER (ORDER BY event_time) LIKE '%' || page_type || '%' THEN ''
ELSE page_type END AS page_type_unique_pre
, page_type
, event_time
FROM (
SELECT string_agg(page_type, ',') OVER (ORDER BY event_time) AS page_type_journey
, first_value(page_type) OVER (PARTITION BY last_page_type_partition ORDER BY event_time) AS last_page_type
, page_type
, event_time
FROM (
SELECT
sum(CASE WHEN page_type IS NULL OR page_type = '' THEN 0 ELSE 1 END) OVER (ORDER BY event_time) AS last_page_type_partition,
page_type,
event_time
FROM (
SELECT * FROM tes
) a
) b
) c
) d;
See test case in this fiddle.
I'm sure there is a better way to achieve this?