I am quite unfamiliar to complex SQL requests ...
What I am trying to achieve is to display of map of active weather alerts on a leaflet map page.
Started by filling a Postgis table with the RSS feed from national weather agencies, then create a view by selecting the ROWs with an expired date later than the actual date and publishing this view through a Geoserver WFS service.
Looked nice except that each geographic area add multiple event-effective-expires features leading to a sort of multiple layers wher only the top event was displayed.
The issue I have is that for each area/polygon I have multiples lines for miscellaneous events/dates that I have to sort out.
The initial table as a structure like :
areadesc(county)|event(type of alert)|effective(date)|expires(date)|severity(low,medium,severe)
What I did to try sort this mess was: first create a view per event to have only the latest expires alert of "such event" per location:
SELECT DISTINCT ON (country.areadesc)
country.areadesc,
country.event,
country.effective,
country.expires,
country.severity
FROM
country
WHERE
country.expires >= now() AND now() >= country.effective
AND country.event::text = 'Thunderstorms'::text
ORDER BY
country.areadesc, country.expires;
I did this for each event I was interested by.
Second I created an aggregated view for each area with the content of the relevant event type :
SELECT DISTINCT
country.areadesc,
country_thunderstorms.severity AS thunderstorms_severity,
country_thunderstorms.effective AS thunderstorms_effective,
country_rain.effective AS rain_effective,
country_rain.expires AS rain_expires,
country_districts.geom
FROM
country_alerts
LEFT JOIN
country_thunderstorms ON country.areadesc::text = country_thunderstorms.areadesc::text
LEFT JOIN
country_wind ON country.areadesc::text = country_wind.areadesc::text
LEFT JOIN
country_rain ON country.areadesc::text = country_rain.areadesc::text
LEFT JOIN
country_districts ON country.areadesc::text = country_districts.name_en::text
WHERE
country.expires >= now() AND now() >= country.effective
AND (country.title::text = ANY (ARRAY['Thunderstorms'::character varying, 'Wind'::character varying, 'Rain'::character varying]::text[]))
ORDER BY
country.areadesc;
All this stuff sort of do the job but looks to quite of a hammer to kill a fly.
I am sure there is a way to achieve this in a single run with nested SELECT but absolutely can't figure how :-(
Any suggestion is welcome , thanks for your help.