DatabaseCreation: 04 Customer data usage history.sql

File 04 Customer data usage history.sql, 1.4 KB (added by 231139, 11 days ago)
Line 
1DROP VIEW IF EXISTS public.v_customer_data_usage_history;
2
3CREATE VIEW public.v_customer_data_usage_history AS
4SELECT
5 c.customer_id,
6 CASE WHEN c.customer_type = 'business' THEN c.company_name ELSE c.first_name || ' ' || c.last_name END AS customer_name,
7 a.account_number,
8 s.subscription_number,
9 p.plan_name,
10 d.data_cdr_id,
11 d.session_start,
12 d.session_end,
13 ROUND(EXTRACT(EPOCH FROM (d.session_end - d.session_start))::numeric / 60.0, 2) AS session_duration_minutes,
14 d.data_used_mb,
15 ROUND(d.data_used_mb / 1024.0, 4) AS data_used_gb,
16 d.apn,
17 d.ip_address,
18 d.charge_amount,
19 d.roaming_partner_id IS NOT NULL AS is_roaming,
20 COALESCE(rp.country, 'North Macedonia') AS network_country,
21 ns.region AS network_region,
22 ns.site_code,
23 ts.sector_label,
24 ts.frequency_band,
25 nt.generation AS network_generation
26FROM public.customers c
27JOIN public.accounts a ON a.customer_id = c.customer_id
28JOIN public.subscriptions s ON s.account_id = a.account_id
29JOIN public.plans p ON p.plan_id = s.plan_id
30JOIN public.usage_cdr_data d ON d.subscription_id = s.subscription_id
31LEFT JOIN public.roaming_partners rp ON rp.roaming_partner_id = d.roaming_partner_id
32LEFT JOIN public.tower_sectors ts ON ts.sector_id = d.sector_id
33LEFT JOIN public.cell_towers ct ON ct.tower_id = ts.tower_id
34LEFT JOIN public.network_sites ns ON ns.site_id = ct.site_id
35LEFT JOIN public.network_technologies nt ON nt.technology_id = ts.technology_id;
36