- Total length of city segments :
SELECT
pt.zip,
SUM(pt.segment_length) AS total_segment_length
FROM (
SELECT DISTINCT
"CITY ZIP CODE" AS zip,
"SEGMENT ID" AS segment_id,
"SEGMENT LENGTH" AS segment_length
FROM potholes_table
) pt
GROUP BY pt.zip
;
SELECT
"CITY ZIP CODE" AS zip,
SUM("MILD POTHOLES" * 10) AS mild,
SUM("IMPORTANT POTHOLES" * 10) AS important,
SUM("SEVERE POTHOLES" * 10) AS severe
FROM potholes_table
GROUP BY "CITY ZIP CODE"
WITH total_length AS (
SELECT
pt.zip,
SUM(pt.segment_length) AS total_segment_length
FROM (
SELECT DISTINCT
"CITY ZIP CODE" AS zip,
"SEGMENT ID" AS segment_id,
"SEGMENT LENGTH" AS segment_length
FROM potholes_table
) pt
GROUP BY pt.zip
),
types_length AS (
SELECT
"CITY ZIP CODE" AS zip,
SUM("MILD POTHOLES" * 10) AS mild,
SUM("IMPORTANT POTHOLES" * 10) AS important,
SUM("SEVERE POTHOLES" * 10) AS severe
FROM potholes_table
GROUP BY "CITY ZIP CODE"
)
SELECT
tot.zip,
tot.total_segment_length,
typ.mild / tot.total_segment_length * 100 AS mild_perc,
typ.important / tot.total_segment_length * 100 AS important_perc,
typ.severe / tot.total_segment_length * 100 AS severe_perc
FROM
total_length tot
INNER JOIN types_length typ ON tot.zip = typ.zip
;
SUM(<pothole_measure> * 10)
if those are fractions (0.0 - 1.0), orSUM(<pothole_measure> * 0.1)
if they are percentages (0 - 100).