| 1 | DROP VIEW IF EXISTS public.v_customer_sms_history;
|
|---|
| 2 |
|
|---|
| 3 | CREATE VIEW public.v_customer_sms_history AS
|
|---|
| 4 | SELECT
|
|---|
| 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 | sms.sms_cdr_id,
|
|---|
| 11 | sms.source_msisdn AS from_number,
|
|---|
| 12 | sms.destination_msisdn AS to_number,
|
|---|
| 13 | sms.event_time AS sent_at,
|
|---|
| 14 | sms.sms_type,
|
|---|
| 15 | sms.direction,
|
|---|
| 16 | sms.charge_amount,
|
|---|
| 17 | sms.roaming_partner_id IS NOT NULL AS is_roaming,
|
|---|
| 18 | COALESCE(rp.country, 'North Macedonia') AS network_country,
|
|---|
| 19 | ns.region AS network_region,
|
|---|
| 20 | ts.sector_label,
|
|---|
| 21 | ts.frequency_band,
|
|---|
| 22 | nt.generation AS network_generation
|
|---|
| 23 | FROM public.customers c
|
|---|
| 24 | JOIN public.accounts a ON a.customer_id = c.customer_id
|
|---|
| 25 | JOIN public.subscriptions s ON s.account_id = a.account_id
|
|---|
| 26 | JOIN public.plans p ON p.plan_id = s.plan_id
|
|---|
| 27 | JOIN public.usage_cdr_sms sms ON sms.subscription_id = s.subscription_id
|
|---|
| 28 | LEFT JOIN public.roaming_partners rp ON rp.roaming_partner_id = sms.roaming_partner_id
|
|---|
| 29 | LEFT JOIN public.tower_sectors ts ON ts.sector_id = sms.sector_id
|
|---|
| 30 | LEFT JOIN public.cell_towers ct ON ct.tower_id = ts.tower_id
|
|---|
| 31 | LEFT JOIN public.network_sites ns ON ns.site_id = ct.site_id
|
|---|
| 32 | LEFT JOIN public.network_technologies nt ON nt.technology_id = ts.technology_id;
|
|---|
| 33 |
|
|---|