--- tickets-export-view

DROP VIEW tickets_export_view;

CREATE VIEW tickets_export_view AS
  SELECT
    t.created_at,
    t.id,

    t.project_id,
    projects.name    AS project,

    t.channel_id,
    channels.title   AS channel,

    t.category_id,
    categories.title AS category,

    t.client_id,
    clients.name     AS client,

    groups.title     AS [group],

    t.assignee_id,
    assignees.name   AS assignee,
    assignees.email  AS assignee_email,

    t.assign_time,
    t.response_time,
    t.total_time,

    t.total_messages,
    t.total_members,

    t.[open],
    t.closed_at

  FROM tickets t
    JOIN projects ON projects.id = t.project_id
    JOIN clients ON clients.id = t.client_id

    LEFT OUTER JOIN channels ON channels.id = t.channel_id
    LEFT OUTER JOIN categories ON categories.id = t.category_id
    LEFT OUTER JOIN groups ON groups.id = t.group_id
    LEFT OUTER JOIN users assignees ON assignees.id = t.assignee_id;
