4545 ("test_wells" , "test well" ),
4646]
4747
48+ LATEST_LOCATION_CTE = """
49+ SELECT DISTINCT ON (lta.thing_id)
50+ lta.thing_id,
51+ lta.location_id,
52+ lta.effective_start
53+ FROM location_thing_association AS lta
54+ WHERE lta.effective_end IS NULL
55+ ORDER BY lta.thing_id, lta.effective_start DESC
56+ """ .strip ()
57+
4858
4959def _safe_view_id (view_id : str ) -> str :
5060 if not re .fullmatch (r"[A-Za-z_][A-Za-z0-9_]*" , view_id ):
@@ -58,13 +68,7 @@ def _create_thing_view(view_id: str, thing_type: str) -> str:
5868 return f"""
5969 CREATE VIEW ogc_{ safe_view_id } AS
6070 WITH latest_location AS (
61- SELECT DISTINCT ON (lta.thing_id)
62- lta.thing_id,
63- lta.location_id,
64- lta.effective_start
65- FROM location_thing_association AS lta
66- WHERE lta.effective_end IS NULL
67- ORDER BY lta.thing_id, lta.effective_start DESC
71+ { LATEST_LOCATION_CTE }
6872 )
6973 SELECT
7074 t.id,
@@ -94,16 +98,10 @@ def _create_thing_view(view_id: str, thing_type: str) -> str:
9498
9599
96100def _create_latest_depth_view () -> str :
97- return """
101+ return f """
98102 CREATE MATERIALIZED VIEW ogc_latest_depth_to_water_wells AS
99103 WITH latest_location AS (
100- SELECT DISTINCT ON (lta.thing_id)
101- lta.thing_id,
102- lta.location_id,
103- lta.effective_start
104- FROM location_thing_association AS lta
105- WHERE lta.effective_end IS NULL
106- ORDER BY lta.thing_id, lta.effective_start DESC
104+ { LATEST_LOCATION_CTE }
107105 ),
108106 ranked_obs AS (
109107 SELECT
@@ -147,16 +145,10 @@ def _create_latest_depth_view() -> str:
147145
148146
149147def _create_avg_tds_view () -> str :
150- return """
148+ return f """
151149 CREATE MATERIALIZED VIEW ogc_avg_tds_wells AS
152150 WITH latest_location AS (
153- SELECT DISTINCT ON (lta.thing_id)
154- lta.thing_id,
155- lta.location_id,
156- lta.effective_start
157- FROM location_thing_association AS lta
158- WHERE lta.effective_end IS NULL
159- ORDER BY lta.thing_id, lta.effective_start DESC
151+ { LATEST_LOCATION_CTE }
160152 ),
161153 tds_obs AS (
162154 SELECT
0 commit comments