| 1 | -- Active customers whose primary address is outside every active sector polygon.
|
|---|
| 2 | -- QGIS point layer. Unique key: address_id; geometry: customer_location.
|
|---|
| 3 | CREATE OR REPLACE VIEW public.v_uncovered_primary_addresses AS
|
|---|
| 4 | SELECT
|
|---|
| 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 | ca.city,
|
|---|
| 10 | ca.country,
|
|---|
| 11 | ca.location AS customer_location
|
|---|
| 12 | FROM public.customers c
|
|---|
| 13 | JOIN public.customer_addresses ca
|
|---|
| 14 | ON ca.customer_id = c.customer_id
|
|---|
| 15 | AND ca.is_primary
|
|---|
| 16 | AND ca.location IS NOT NULL
|
|---|
| 17 | WHERE c.status = 'active'
|
|---|
| 18 | AND NOT EXISTS
|
|---|
| 19 | (
|
|---|
| 20 | SELECT 1
|
|---|
| 21 | FROM public.coverage_zones cz
|
|---|
| 22 | JOIN public.tower_sectors ts ON ts.sector_id = cz.sector_id
|
|---|
| 23 | JOIN public.cell_towers ct ON ct.tower_id = ts.tower_id
|
|---|
| 24 | JOIN public.network_sites ns ON ns.site_id = ct.site_id
|
|---|
| 25 | WHERE cz.coverage_area IS NOT NULL
|
|---|
| 26 | AND ns.status = 'active'
|
|---|
| 27 | AND ct.status = 'active'
|
|---|
| 28 | AND ts.status = 'active'
|
|---|
| 29 | AND ST_Covers(cz.coverage_area, ca.location)
|
|---|
| 30 | );
|
|---|