-- Count total rows in crash locations table
SELECT count(*)
from vision_zero_crash
WHERE "YEAR" >= 2011
count |
---|
64188 |
-- (Panel) Crash details for map
-- reduce violations field to single variable
create or replace function violations_reducer(a text) RETURNS text AS $$
BEGIN
IF position(',' in a) > 0 then
DECLARE
v text;
arr text[] = string_to_array(a,',');
BEGIN
FOREACH v IN ARRAY arr
LOOP
IF v != 'Unknown' then
return trim(leading ' ' from v);
END IF;
END LOOP;
END;
ELSE
return a;
END IF;
END;
$$ language plpgsql;
SELECT
to_date("CRASHDATETIME",'YYYY/MM/DD') as "CRASHDATE",
violations_reducer("VEHICLEVIOLATIONCODEDESCRIPTION") as "VIOLATION",
(CASE
WHEN SPLIT_PART("AGERANGE", ',', 1) != 'NULL' then SPLIT_PART("AGERANGE", ',', 1)
else '30-49' end) as "AGE",
(CASE
WHEN "LIGHTING" = 'Dusk - Dawn' then 'Dusk/Dawn'
WHEN "LIGHTING" LIKE 'Dark%' then 'Dark'
else 'Daylight' end) as "LIGHTING",
(CASE
WHEN "VEHICLEPARTYCATEGORY" LIKE '%Pedestrian%' then 'Pedestrian'
WHEN "VEHICLEPARTYCATEGORY" LIKE '%Bicycle%' then 'Bicycle'
else 'Vehicle' end) as "INVOLVING",
"WEATHER",
"DAYOFWEEKNAME",
"NARRATIVE",
"VEHICLEINVOLVEDWITH",
"COLLISIONTYPE",
"MINORINJURIES",
"MODERATEINJURIES",
"SEVEREINJURIES",
"FATALINJURIES",
"ROADWAYCONDITION",
"ROADWAYSURFACE",
"INTASTREETNAME",
"INTBSTREETNAME",
"INTERSECTIONNUMBERINT",
"PROXIMITYTOINTERSECTION",
"LATITUDE", "LONGITUDE"
FROM vision_zero_crash
WHERE "YEAR" >= 2011
ORDER by "CRASHDATETIME"::date
CRASHDATE | VIOLATION | AGE | LIGHTING | INVOLVING | WEATHER | DAYOFWEEKNAME | NARRATIVE | VEHICLEINVOLVEDWITH | COLLISIONTYPE | MINORINJURIES | MODERATEINJURIES | SEVEREINJURIES | FATALINJURIES | ROADWAYCONDITION | ROADWAYSURFACE | INTASTREETNAME | INTBSTREETNAME | INTERSECTIONNUMBERINT | PROXIMITYTOINTERSECTION | LATITUDE | LONGITUDE |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2011-01-01 | Unsafe U-Turn | 16-29 | Dark | Vehicle | Clear | Saturday | 29 year-old female driver vs unspecified driver | Other Vehicle | Rear End | 0 | 0 | 0 | 0 | No Unusual Conditions | Dry | ALMA AV | PALM ST | 4168 | Non-Related | 37.3115987 | -121.8808112 |
2011-01-01 | Speeding | 30-49 | Dark | Vehicle | Rain | Saturday | Unspecified driver vs parked car | Parked Vehicle | Other | 0 | 0 | 0 | 0 | No Unusual Conditions | Wet | MANZANITA DR | SARATOGA AV | 87460 | Non-Related | 37.31164303 | -121.97861969 |
2011-01-01 | Speeding | 16-29 | Daylight | Vehicle | Cloudy | Saturday | 21 year-old female driver vs object | Fixed Object | Other | 0 | 1 | 0 | 0 | No Unusual Conditions | Wet | BLOSSOM HILL RD/DW | COTTLE RD | 42668 | Non-Related | 37.25377914 | -121.8031774 |
2011-01-01 | Speeding | 30-49 | Daylight | Vehicle | Rain | Saturday | Unspecified driver vs object | Fixed Object | Other | 0 | 0 | 0 | 0 | No Unusual Conditions | Wet | DELMAS AV/SHEPHERD AV | WILLOW ST | 50214 | Non-Related | 37.31217342 | -121.8938377 |
2011-01-01 | Speeding | 30-49 | Dusk/Dawn | Vehicle | Cloudy | Saturday | 40 year-old male drunk truck driver vs 29 year-old male driver | Other Vehicle | Broadside | 0 | 0 | 0 | 0 | No Unusual Conditions | Wet | BAYSHORE FR | JULIAN ST | 14960 | Non-Related | 37.35317711 | -121.8672439 |
2011-01-01 | Unsafe Turn Movement | 16-29 | Daylight | Vehicle | Rain | Saturday | 28 year-old male drunk driver vs object | Fixed Object | Other | 0 | 1 | 0 | 0 | No Unusual Conditions | Wet | CAPITOL EX | QUIMBY RD | 32922 | Non-Related | 37.3238552 | -121.8084406 |
2011-01-01 | Unsafe Turn Movement | 30-49 | Dark | Vehicle | Cloudy | Saturday | Unspecified driver vs 2 parked cars | Parked Vehicle | Other | 0 | 0 | 0 | 0 | No Unusual Conditions | Dry | ESTEBAN WY | SOLANO DR | 57530 | Non-Related | 37.23211429 | -121.7981208 |
2011-01-01 | Unknown | 65 plus | Daylight | Vehicle | Clear | Saturday | 84 year-old male driver vs unspecified driver | Other Vehicle | Rear End | 0 | 0 | 0 | 0 | No Unusual Conditions | Dry | KIELY BL | SARATOGA AV | 79188 | Non-Related | 37.32015115 | -121.9724316 |
2011-01-01 | Unknown | 16-29 | Dark | Pedestrian | Rain | Saturday | 44 year-old male driver vs 21 year-old male pedestrian with serious injury | Pedestrian | Other | 0 | 0 | 1 | 0 | No Unusual Conditions | Wet | ELEVENTH ST | SAN CARLOS ST | 55813 | Non-Related | 37.33649044 | -121.8764777 |
2011-01-01 | Speeding | 30-49 | Dusk/Dawn | Vehicle | Rain | Saturday | Unspecified tipsy driver vs 2 parked cars | Parked Vehicle | Other | 0 | 0 | 0 | 0 | No Unusual Conditions | Wet | MENKER AV | PARKMOOR AV | 90474 | Non-Related | 37.31720424 | -121.9216311 |
-- (Panel) Corridor Specific safety details
ALTER TABLE safety_corridors
ADD COLUMN IF NOT EXISTS "STREETNAME" VARCHAR;
UPDATE safety_corridors
SET "STREETNAME" = UPPER("SAFETYPRIO");
UPDATE safety_corridors
SET "STREETNAME" = REPLACE("STREETNAME", 'AVE', 'AV');
UPDATE safety_corridors
SET "STREETNAME" = REPLACE("STREETNAME", 'EXPRESSWAY', 'EX');
UPDATE safety_corridors
SET "STREETNAME" = REPLACE("STREETNAME", 'ROAD', 'RD');
UPDATE safety_corridors
SET "STREETNAME" = REPLACE("STREETNAME", 'STREET', 'ST');
UPDATE safety_corridors
SET "STREETNAME" = REPLACE("STREETNAME", 'LANE', 'LN');
-- SELECT *
-- FROM safety_corridors
-- ORDER BY 1;
-- (Panel) Top 60 Safety Corridors \w moving average and rolling total
BEGIN;
CREATE TEMPORARY TABLE Astreet ON COMMIT DROP
as SELECT
SUM("MINORINJURIES") as minorA,
SUM("MODERATEINJURIES") as moderateA,
SUM("SEVEREINJURIES") as severeA,
SUM("FATALINJURIES") as fatalA,
"INTASTREETNAME",
"YEAR"
FROM vision_zero_crash
WHERE "YEAR" >= 2011
GROUP BY "INTASTREETNAME", "YEAR";
CREATE TEMPORARY TABLE Bstreet ON COMMIT DROP
as SELECT
SUM("MINORINJURIES") as minorB,
SUM("MODERATEINJURIES") as moderateB,
SUM("SEVEREINJURIES") as severeB,
SUM("FATALINJURIES") as fatalB,
"INTBSTREETNAME",
"YEAR"
FROM vision_zero_crash
WHERE "YEAR" >= 2011
GROUP BY "INTBSTREETNAME", "YEAR";
CREATE TEMPORARY TABLE crashes ON COMMIT DROP
as SELECT
CASE WHEN bstreet."INTBSTREETNAME" IS NULL THEN astreet."INTASTREETNAME" ELSE bstreet."INTBSTREETNAME" END AS "STREETNAME",
(coalesce(astreet."YEAR", bstreet."YEAR")) as "YEAR",
(coalesce(astreet.minorA, 0) + coalesce(bstreet.minorB, 0)) as "MINORINJURIES",
(coalesce(astreet.moderateA, 0) + coalesce(bstreet.moderateB, 0)) as "MODERATEINJURIES",
(coalesce(astreet.severeA, 0) + coalesce(bstreet.severeB, 0)) as "SEVEREINJURIES",
(coalesce(astreet.fatalA, 0) + coalesce(bstreet.fatalB, 0)) as "FATALINJURIES",
(coalesce(astreet.minorA, 0) + coalesce(bstreet.minorB, 0) + coalesce(astreet.moderateA, 0) + coalesce(bstreet.moderateB, 0)) as "MMI",
(coalesce(astreet.fatalA, 0) + coalesce(bstreet.fatalB, 0) + coalesce(astreet.severeA, 0) + coalesce(bstreet.severeB, 0)) as "KSI"
FROM Astreet astreet
full outer join Bstreet bstreet
ON bstreet."INTBSTREETNAME" = astreet."INTASTREETNAME"
and bstreet."YEAR" = astreet."YEAR"
ORDER BY "STREETNAME", "YEAR";
CREATE TEMPORARY TABLE topstreets ON COMMIT DROP
as SELECT
(CASE
WHEN "STREETNAME" in (
'MONTEREY RD',
'ALUM ROCK AV',
'CAPITOL EX',
'BLOSSOM HILL RD',
'JACKSON AV',
'MCKEE RD',
'SENTER RD',
'TULLY RD',
'WHITE RD',
'ALMADEN EX',
'MCLAUGHLIN AV',
'BRANHAM LN',
'KING RD',
'STORY RD',
'SANTA CLARA ST',
'SARATOGA AV',
'FRUITDALE AV',
'HILLSDALE AV',
'FIRST ST') then 'True'
else 'False' end) as "PRIORITY",
SUM("KSI") as "KSI", "STREETNAME"
FROM crashes
GROUP BY "STREETNAME"
ORDER BY "KSI" DESC
LIMIT 60;
CREATE TEMPORARY TABLE safetycorridors ON COMMIT DROP
as SELECT *
from topstreets
where "KSI" > 30 or "PRIORITY" = 'True';
SELECT *,
SUM("MINORINJURIES") OVER(PARTITION BY "STREETNAME" ORDER BY "STREETNAME", "YEAR") AS "ROLL_TOT_MIN",
SUM("MODERATEINJURIES") OVER(PARTITION BY "STREETNAME" ORDER BY "STREETNAME", "YEAR") AS "ROLL_TOT_MOD",
SUM("SEVEREINJURIES") OVER(PARTITION BY "STREETNAME" ORDER BY "STREETNAME", "YEAR") AS "ROLL_TOT_SEV",
SUM("FATALINJURIES") OVER(PARTITION BY "STREETNAME" ORDER BY "STREETNAME", "YEAR") AS "ROLL_TOT_FATAL",
SUM("MMI") OVER(PARTITION BY "STREETNAME" ORDER BY "STREETNAME", "YEAR") AS "ROLL_TOT_MMI",
SUM("KSI") OVER(PARTITION BY "STREETNAME" ORDER BY "STREETNAME", "YEAR") AS "ROLL_TOT_KSI"
FROM crashes
WHERE "STREETNAME" IN (select "STREETNAME" from safetycorridors)
ORDER BY "STREETNAME", "YEAR";
COMMIT;
STREETNAME | YEAR | MINORINJURIES | MODERATEINJURIES | SEVEREINJURIES | FATALINJURIES | MMI | KSI | ROLL_TOT_MIN | ROLL_TOT_MOD | ROLL_TOT_SEV | ROLL_TOT_FATAL | ROLL_TOT_MMI | ROLL_TOT_KSI |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ALMADEN EX | 2011 | 76 | 16 | 0 | 0 | 92 | 0 | 76 | 16 | 0 | 0 | 92 | 0 |
ALMADEN EX | 2012 | 59 | 19 | 7 | 2 | 78 | 9 | 135 | 35 | 7 | 2 | 170 | 9 |
ALMADEN EX | 2013 | 39 | 7 | 3 | 2 | 46 | 5 | 174 | 42 | 10 | 4 | 216 | 14 |
ALMADEN EX | 2014 | 30 | 22 | 5 | 4 | 52 | 9 | 204 | 64 | 15 | 8 | 268 | 23 |
ALMADEN EX | 2015 | 22 | 12 | 2 | 4 | 34 | 6 | 226 | 76 | 17 | 12 | 302 | 29 |
ALMADEN EX | 2016 | 51 | 10 | 4 | 0 | 61 | 4 | 277 | 86 | 21 | 12 | 363 | 33 |
ALMADEN EX | 2017 | 32 | 9 | 1 | 1 | 41 | 2 | 309 | 95 | 22 | 13 | 404 | 35 |
ALMADEN EX | 2018 | 41 | 8 | 4 | 0 | 49 | 4 | 350 | 103 | 26 | 13 | 453 | 39 |
ALMADEN EX | 2019 | 26 | 25 | 5 | 2 | 51 | 7 | 376 | 128 | 31 | 15 | 504 | 46 |
ALMADEN EX | 2020 | 31 | 14 | 4 | 3 | 45 | 7 | 407 | 142 | 35 | 18 | 549 | 53 |
ALMADEN EX | 2021 | 37 | 12 | 4 | 0 | 49 | 4 | 444 | 154 | 39 | 18 | 598 | 57 |
ALMADEN EX | 2022 | 23 | 10 | 5 | 4 | 33 | 9 | 467 | 164 | 44 | 22 | 631 | 66 |
ALUM ROCK AV | 2011 | 49 | 16 | 6 | 0 | 65 | 6 | 49 | 16 | 6 | 0 | 65 | 6 |