This SQL code performs the following operations on the `haz_model_concept.vuln_asset_scores`...
This SQL code performs the following operations on the haz_model_concept.vuln_asset_scores
table, specifically dealing with assets related to a vulnerability parameter wind_mph_100_yr_its
:
Step 1: Delete Existing Entries
The DELETE
statement removes all rows from the vuln_asset_scores
table where the vuln_param_name
column is equal to 'wind_mph_100_yr_its'
. This step ensures that only fresh, up-to-date data for this specific vulnerability parameter will exist after the subsequent operations.
DELETE FROM haz_model_concept.vuln_asset_scores
WHERE vuln_param_name = 'wind_mph_100_yr_its';
Step 2: Insert New Data
The INSERT INTO
statement adds new data to the vuln_asset_scores
table. The new rows are generated using the SELECT
statement that joins multiple related tables and filters rows based on specific conditions. Here's what the SELECT
query does:
INSERT INTO haz_model_concept.vuln_asset_scores (asset_id, vuln_param_name, vuln_param_score)
SELECT DISTINCT ON (fa.asset_id) ...
-
Retrieve Data Distinct by Asset (
DISTINCT ON (fa.asset_id)
):- The
DISTINCT ON
clause ensures only one row perfa.asset_id
(asset ID) is selected. This is based on a preference usingORDER BY vat.threshold_score DESC
, which keeps the row with the highestthreshold_score
for each asset.
- The
-
Select Relevant Columns:
fa.asset_id
: The ID of the asset.vap.vuln_param_name
: The vulnerability parameter name (wind_mph_100_yr_its
).vat.threshold_score
: The threshold score based on hazard data.
-
Joins Across Multiple Tables:
- The query combines several related tables via
INNER JOIN
to gather all the necessary data:feats_asset fa
: Provides details about assets.sample_points sp
: Links assets to sample points.sample_haz_vals shv
: Provides hazard values at sample points.sources_haz sh
: Provides hazard source details.vuln_asset_thresholds vat
: Defines vulnerability thresholds.vuln_asset_params vap
: Provides metadata for vulnerability parameters.
- The query combines several related tables via
-
Filter Conditions (
WHERE
Clause):- The
WHERE
clause ensures only rows meeting specific conditions are included:vap.inverse IS FALSE
: Only rows whereinverse
is false are included (some kind of filtering based on parameter directionality).vap.vuln_param_name = 'wind_mph_100_yr_its'
: Includes only the target vulnerability parameter.sh.source_haz_label = 'Wind Contour_100 yr'
: Filters data to the appropriate hazard category.fa.asset_type_prim = 'its'
: Further narrows scope to assets of type'its'
.
- The
-
Row Preference (
ORDER BY
Clause):- The
ORDER BY fa.asset_id, vat.threshold_score DESC
ensures that within each asset, rows are sorted by threshold score in descending order. This allows theDISTINCT ON
clause to choose the row with the highest threshold score for each asset.
- The
Summary:
- Delete Existing Rows: The code removes all existing rows with
vuln_param_name = 'wind_mph_100_yr_its'
from thevuln_asset_scores
table. - Insert Fresh Data: It calculates and inserts scores for vulnerability parameter
'wind_mph_100_yr_its'
into thehaz_model_concept.vuln_asset_scores
table. For each asset, the row with the highestthreshold_score
that meets the specified conditions is used.
This process effectively updates the vuln_asset_scores
table to reflect the most up-to-date scores for the 'wind_mph_100_yr_its'
parameter, focusing on assets of type 'its'
and using hazard data related to "Wind Contour 100 yr."