| 1 | create view public.v_customer_data_usage_history
|
|---|
| 2 | (customer_id, customer_name, email, account_number, subscription_number, plan_name, contract_number,
|
|---|
| 3 | contract_status, data_cdr_id, session_start, session_end, session_duration_minutes, data_used_mb,
|
|---|
| 4 | data_used_gb, apn, ip_address, charge_amount, is_roaming, network_country, sector_label, frequency_band,
|
|---|
| 5 | network_generation)
|
|---|
| 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 | a.account_number,
|
|---|
| 11 | s.subscription_number,
|
|---|
| 12 | p.plan_name,
|
|---|
| 13 | con.contract_number,
|
|---|
| 14 | con.status AS contract_status,
|
|---|
| 15 | d.data_cdr_id,
|
|---|
| 16 | d.session_start,
|
|---|
| 17 | d.session_end,
|
|---|
| 18 | round(EXTRACT(epoch FROM d.session_end - d.session_start) / 60.0, 2) AS session_duration_minutes,
|
|---|
| 19 | d.data_used_mb,
|
|---|
| 20 | round(d.data_used_mb / 1024.0, 4) AS data_used_gb,
|
|---|
| 21 | d.apn,
|
|---|
| 22 | d.ip_address,
|
|---|
| 23 | d.charge_amount,
|
|---|
| 24 | CASE
|
|---|
| 25 | WHEN d.roaming_partner_id IS NOT NULL THEN true
|
|---|
| 26 | ELSE false
|
|---|
| 27 | END AS is_roaming,
|
|---|
| 28 | COALESCE(rp.country, 'Home'::text) AS network_country,
|
|---|
| 29 | ts.sector_label,
|
|---|
| 30 | ts.frequency_band,
|
|---|
| 31 | nt.generation AS network_generation
|
|---|
| 32 | FROM customers c
|
|---|
| 33 | JOIN accounts a ON a.customer_id = c.customer_id
|
|---|
| 34 | JOIN subscriptions s ON s.account_id = a.account_id
|
|---|
| 35 | JOIN plans p ON p.plan_id = s.plan_id
|
|---|
| 36 | LEFT JOIN contracts con ON con.contract_id = s.contract_id
|
|---|
| 37 | JOIN usage_cdr_data d ON d.subscription_id = s.subscription_id
|
|---|
| 38 | LEFT JOIN roaming_partners rp ON rp.roaming_partner_id = d.roaming_partner_id
|
|---|
| 39 | LEFT JOIN tower_sectors ts ON ts.sector_id = d.sector_id
|
|---|
| 40 | LEFT JOIN network_technologies nt ON nt.technology_id = ts.technology_id;
|
|---|
| 41 |
|
|---|
| 42 | alter table public.v_customer_data_usage_history
|
|---|
| 43 | owner to postgres;
|
|---|