간에 따라 활동 중인 의원, 휴면 중인 의원, 이탈한 의원으로 구분하고 그 분포를 확인합니다.
SELECT
case
when
months_since_last <= 23
then
‘Current’
when
months_since_last <= 48
then
‘Lapsed’
else
‘Churned’
end
as
status
,
sum
(reps)
as
total_reps
FROM
(
SELECT
date_part(‘year’,interval_since_last) * 12
+ date_part(‘month’,interval_since_last)
as
months_since_last
,
count
(*)
as
reps
FROM
(
SELECT
id_bioguide
,
max
(term_start)
as
max_date
,age(‘2020-05-19’,
max
(term_start))
as
interval_since_last
FROM
legislators_terms
WHERE
term_type = ‘rep’
GROUP
BY
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.