| 1 | create view public.v_customer_subscription_status_timeline
|
|---|
| 2 | (customer_id, customer_name, email, account_number, subscription_number, plan_name, status_history_id,
|
|---|
| 3 | old_status, new_status, changed_at, reason, changed_by_employee)
|
|---|
| 4 | as
|
|---|
| 5 | SELECT c.customer_id,
|
|---|
| 6 | COALESCE((c.first_name || ' '::text) || c.last_name, c.company_name) AS customer_name,
|
|---|
| 7 | c.email,
|
|---|
| 8 | a.account_number,
|
|---|
| 9 | s.subscription_number,
|
|---|
| 10 | p.plan_name,
|
|---|
| 11 | ssh.status_history_id,
|
|---|
| 12 | ssh.old_status,
|
|---|
| 13 | ssh.new_status,
|
|---|
| 14 | ssh.changed_at,
|
|---|
| 15 | ssh.reason,
|
|---|
| 16 | (e.first_name || ' '::text) || e.last_name AS changed_by_employee
|
|---|
| 17 | FROM customers c
|
|---|
| 18 | JOIN accounts a ON a.customer_id = c.customer_id
|
|---|
| 19 | JOIN subscriptions s ON s.account_id = a.account_id
|
|---|
| 20 | JOIN plans p ON p.plan_id = s.plan_id
|
|---|
| 21 | JOIN subscription_status_history ssh ON ssh.subscription_id = s.subscription_id
|
|---|
| 22 | LEFT JOIN employees e ON e.employee_id = ssh.changed_by_employee_id;
|
|---|
| 23 |
|
|---|
| 24 | alter table public.v_customer_subscription_status_timeline
|
|---|
| 25 | owner to postgres;
|
|---|