-- P5-5190: add datastudio view for inquiries

DROP VIEW IF EXISTS v_analytics_inquiries;

CREATE VIEW v_analytics_inquiries AS
SELECT
    inquiryid,
    inquiries.inserted,
    inquiries.updated
    company,
    firstname,
    lastname
    email,
    territory,
    product_group,
    price_m2,
    price_effect,
    price_retouch,
    width,
    height,
    "artNo",
    materialid,
    zendesk_ticket,
    order_row.rowid AS order_row_id,
    order_row.orderid AS order_id
FROM inquiries
LEFT JOIN "order-rows_details" order_row_inquiry ON order_row_inquiry.fieldid = 114 AND order_row_inquiry.value::numeric = inquiries.inquiryid
LEFT JOIN "order-rows" order_row ON order_row.rowid = order_row_inquiry.rowid
ORDER BY inserted DESC;


GRANT SELECT ON v_analytics_inquiries TO datastudio;
