这是下面的查询:
SELECT
e.country_name,
extract(year FROM e.e_date) AS year,
sum(e.vote_share) AS vote_share
FROM
election e
JOIN party p ON e.party_id = p.id
WHERE
e.e_type = 'parliament'
AND p.family_name IN ('Green/Ecologist')
AND e.country_name = 'Austria'
AND e.e_date >= '1980-01-01'::date
AND e.e_date < '2020-01-01'::date
GROUP BY
e.country_name,
e.e_date,
e.vote_share;
你在vote_share上求和,很好,但不要在vote_share上分组。
这样可以做到:
SELECT
e.country_name,
extract(year FROM e.e_date) AS year,
sum(e.vote_share) AS vote_share
FROM
election e
JOIN party p ON e.party_id = p.id
WHERE
e.e_type = 'parliament'
AND p.family_name IN ('Green/Ecologist')
AND e.country_name = 'Austria'
AND e.e_date >= '1980-01-01'::date
AND e.e_date < '2020-01-01'::date
GROUP BY
e.country_name,
e.e_date