DROP VIEW IF EXISTS public.v_analytics_orders_rows_not_delivered_with_paint; CREATE VIEW public.v_analytics_orders_rows_not_delivered_with_paint AS SELECT order_rows.id AS order_row_id, orders.id AS orderid, orders.inserted AS order_date, CASE WHEN order_rows.sub_type IN ('paint_card_sample', 'wall_paint') THEN order_rows.sub_type::text WHEN order_rows.type IN ('sample'::order_row_type, 'stock'::order_row_type) THEN order_rows.type::text ELSE order_rows.data ->> 'group'::text END AS product_group, order_rows.data ->> 'artNo'::text AS artno, order_rows.status::text AS status, order_rows.data ->> 'name'::text AS name, order_rows.data ->> 'material'::text AS material, CASE WHEN sub_type IN ('paint_card_sample', 'wall_paint') THEN order_rows.data ->> 'name' ELSE null END AS paint_name, CASE WHEN sub_type IN ('wall_paint') THEN (order_rows.data ->> 'paintLiters')::numeric ELSE null END AS paint_liters, CASE WHEN sub_type IN ('paint_card_sample', 'wall_paint') THEN order_rows.data ->> 'paintColor' ELSE null END AS paint_ncs FROM order_rows JOIN orders ON order_rows.order_id = orders.id WHERE (orders.confirmed = 1 AND orders.delivered = 0 AND orders.canceled = 0 AND orders.paid = 1) AND ( order_rows.type IN ( 'print'::order_row_type, 'sample'::order_row_type, 'stock'::order_row_type ) OR order_rows.sub_type IN ( 'paint_card_sample', 'wall_paint' ) ) AND NOT order_rows.type = 'refund';