Advanced: 05 Open outage customer impact.sql

File 05 Open outage customer impact.sql, 1.4 KB (added by 231139, 11 days ago)
Line 
1-- Customers whose primary address lies in a sector belonging to an open-outage site.
2-- QGIS point layer. Geometry: customer_location.
3CREATE OR REPLACE VIEW public.v_open_outage_customer_impact AS
4SELECT
5 o.outage_id,
6 o.site_id,
7 ns.site_code,
8 ns.site_name,
9 o.outage_type,
10 o.start_time,
11 c.customer_id,
12 ca.address_id,
13 concat_ws(', ', ca.street, ca.city, ca.country) AS customer_address,
14 count(DISTINCT cz.coverage_zone_id) AS affected_coverage_zones,
15 round(
16 ST_Distance(ca.location::geography, ns.location::geography)::numeric,
17 1
18 ) AS distance_to_site_m,
19 ca.location AS customer_location
20FROM public.outages o
21JOIN public.network_sites ns ON ns.site_id = o.site_id
22JOIN public.cell_towers ct ON ct.site_id = ns.site_id
23JOIN public.tower_sectors ts ON ts.tower_id = ct.tower_id
24JOIN public.coverage_zones cz ON cz.sector_id = ts.sector_id
25JOIN public.customer_addresses ca
26 ON ca.is_primary
27 AND ca.location IS NOT NULL
28 AND cz.coverage_area IS NOT NULL
29 AND ST_Covers(cz.coverage_area, ca.location)
30JOIN public.customers c
31 ON c.customer_id = ca.customer_id
32 AND c.status = 'active'
33WHERE o.status = 'open'
34GROUP BY
35 o.outage_id,
36 o.site_id,
37 ns.site_code,
38 ns.site_name,
39 o.outage_type,
40 o.start_time,
41 c.customer_id,
42 ca.address_id,
43 ca.street,
44 ca.city,
45 ca.country,
46 ca.location,
47 ns.location;