Advanced: 04 Customer coverage detail.sql

File 04 Customer coverage detail.sql, 1.3 KB (added by 231139, 10 days ago)
Line 
1-- One row for every active sector covering an active customer's primary address.
2-- QGIS point layer. Unique key can be customer_id + coverage_zone_id.
3CREATE OR REPLACE VIEW public.v_customer_coverage_detail AS
4SELECT
5 c.customer_id,
6 concat_ws(' ', c.first_name, c.last_name) AS customer_name,
7 ca.address_id,
8 concat_ws(', ', ca.street, ca.city, ca.country) AS full_address,
9 cz.coverage_zone_id,
10 ns.site_id,
11 ns.site_code,
12 ns.site_name,
13 ct.tower_code,
14 ts.sector_id,
15 ts.sector_label,
16 nt.technology_name,
17 nt.generation,
18 cz.signal_quality_score,
19 round(
20 ST_Distance(ca.location::geography, ns.location::geography)::numeric,
21 1
22 ) AS distance_to_site_m,
23 ca.location AS customer_location
24FROM public.customers c
25JOIN public.customer_addresses ca
26 ON ca.customer_id = c.customer_id
27 AND ca.is_primary
28JOIN public.coverage_zones cz
29 ON ca.location IS NOT NULL
30 AND cz.coverage_area IS NOT NULL
31 AND ST_Covers(cz.coverage_area, ca.location)
32JOIN public.tower_sectors ts ON ts.sector_id = cz.sector_id
33JOIN public.cell_towers ct ON ct.tower_id = ts.tower_id
34JOIN public.network_sites ns ON ns.site_id = ct.site_id
35JOIN public.network_technologies nt
36 ON nt.technology_id = ts.technology_id
37WHERE c.status = 'active'
38 AND ns.status = 'active'
39 AND ct.status = 'active'
40 AND ts.status = 'active';