Files
Rikard BartholfandGitHub 376246992a Add payment_method to v_analytics_orders2 (#422)
* Add payment_method to v_analytics_orders2

* Fetch payment_type from Adyen if applicable, else from orders

* Set correct version
2024-10-15 12:00:58 +02:00

71 lines
2.7 KiB
SQL

-- View: public.v_analytics_orders2
DROP VIEW public.v_analytics_orders2;
CREATE VIEW public.v_analytics_orders2
AS
SELECT orders.id AS order_id,
orders.inserted AS order_date,
orders.delivered,
orders.newsletter,
orders.credit_invoice,
orders.canceled,
orders.reminder,
orders.delivered_date,
orders.country_code,
orders.language,
orders.delivery_country_code,
orders.delivery_method,
orders.delivery_price,
orders.customer_type,
COALESCE(pm.payment_method, orders.payment_type) payment_type,
orders.currency,
orders.exchange_rate,
orders.exchange_rate_eur,
orders.invoice_id,
orders.contract_customer_id,
orders.vat_number,
orders.cancels_order_id,
orders.cancelled_by_order_id,
orders.reseller_store,
orders.order_sum,
orders.order_sum / orders.exchange_rate AS order_sum_sek,
billing_address.email,
billing_address.phone,
billing_address.address1 AS billing_address_address1,
billing_address.address2 AS billing_address_address2,
billing_address.company_name AS billing_address_company_name,
billing_address.country_code AS billing_address_country_code,
billing_address.city AS billing_address_city,
billing_address.zipcode AS billing_address_zipcode,
billing_address.first_name AS billing_address_first_name,
billing_address.last_name AS billing_address_last_name,
billing_address.state AS billing_address_state,
delivery_address.address1 AS delivery_address_address1,
delivery_address.address2 AS delivery_address_address2,
delivery_address.company_name AS delivery_address_company_name,
delivery_address.country_code AS delivery_address_country_code,
delivery_address.city AS delivery_address_city,
delivery_address.zipcode AS delivery_address_zipcode,
delivery_address.first_name AS delivery_address_first_name,
delivery_address.last_name AS delivery_address_last_name,
delivery_address.state AS delivery_address_state
FROM orders
JOIN addresses billing_address ON billing_address.id = orders.billing_address_id
LEFT JOIN (
SELECT order_id,
(authorisation_webhook::jsonb)->'notificationItems'->0->'NotificationRequestItem'->>'paymentMethod' payment_method
FROM adyen_sessions JOIN orders ON order_id = orders.id
WHERE order_id IS NOT NULL
) pm ON orders.id = pm.order_id
JOIN addresses delivery_address
ON delivery_address.id = orders.delivery_address_id
WHERE orders.paid = 1;
ALTER TABLE public.v_analytics_orders2
OWNER TO root;
GRANT SELECT ON TABLE public.v_analytics_orders2 TO datastudio;
GRANT DELETE, UPDATE, SELECT, INSERT ON TABLE public.v_analytics_orders2 TO photowall;
GRANT ALL ON TABLE public.v_analytics_orders2 TO root;