| 1 | create view public.v_customer_device_sim_history
|
|---|
| 2 | (customer_id, customer_name, email, customer_type, account_number, subscription_number, plan_name,
|
|---|
| 3 | contract_number, device_active_from, device_active_to, device_assignment_status, manufacturer,
|
|---|
| 4 | device_model, device_type, sim_active_from, sim_active_to, sim_assignment_status, phone_number, sim_type,
|
|---|
| 5 | sim_card_status)
|
|---|
| 6 | as
|
|---|
| 7 | SELECT c.customer_id,
|
|---|
| 8 | COALESCE((c.first_name || ' '::text) || c.last_name, c.company_name) AS customer_name,
|
|---|
| 9 | c.email,
|
|---|
| 10 | c.customer_type,
|
|---|
| 11 | a.account_number,
|
|---|
| 12 | s.subscription_number,
|
|---|
| 13 | p.plan_name,
|
|---|
| 14 | con.contract_number,
|
|---|
| 15 | da.assigned_from AS device_active_from,
|
|---|
| 16 | da.assigned_to AS device_active_to,
|
|---|
| 17 | CASE
|
|---|
| 18 | WHEN da.device_assignment_id IS NULL THEN 'no_operator_device'::text
|
|---|
| 19 | WHEN da.assigned_to IS NULL THEN 'active_device_assignment'::text
|
|---|
| 20 | ELSE 'historical_device_assignment'::text
|
|---|
| 21 | END AS device_assignment_status,
|
|---|
| 22 | dev.manufacturer,
|
|---|
| 23 | dev.model AS device_model,
|
|---|
| 24 | dev.device_type,
|
|---|
| 25 | ssh.start_date AS sim_active_from,
|
|---|
| 26 | ssh.end_date AS sim_active_to,
|
|---|
| 27 | CASE
|
|---|
| 28 | WHEN ssh.sim_card_subscription_history_id IS NULL THEN 'no_sim_history'::text
|
|---|
| 29 | WHEN ssh.end_date IS NULL THEN 'active_sim_assignment'::text
|
|---|
| 30 | ELSE 'historical_sim_assignment'::text
|
|---|
| 31 | END AS sim_assignment_status,
|
|---|
| 32 | sc.msisdn AS phone_number,
|
|---|
| 33 | sc.sim_type,
|
|---|
| 34 | sc.status AS sim_card_status
|
|---|
| 35 | FROM customers c
|
|---|
| 36 | JOIN accounts a ON a.customer_id = c.customer_id
|
|---|
| 37 | JOIN subscriptions s ON s.account_id = a.account_id
|
|---|
| 38 | JOIN plans p ON p.plan_id = s.plan_id
|
|---|
| 39 | LEFT JOIN contracts con ON con.contract_id = s.contract_id
|
|---|
| 40 | LEFT JOIN device_assignments da ON da.subscription_id = s.subscription_id
|
|---|
| 41 | LEFT JOIN devices dev ON dev.device_id = da.device_id
|
|---|
| 42 | LEFT JOIN sim_card_subscription_history ssh ON ssh.subscription_id = s.subscription_id
|
|---|
| 43 | LEFT JOIN sim_cards sc ON sc.sim_id = ssh.sim_id;
|
|---|
| 44 |
|
|---|
| 45 | alter table public.v_customer_device_sim_history
|
|---|
| 46 | owner to postgres;
|
|---|