조인 더 연습해 보기
이전 연습 문제에서 만든 select 문을 그대로 사용할 수 있지만,
이번에는 몇 개 열만 반환하고 다른 테이블을 group_by() 절에 사용해 보겠습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 데이터베이스 입문
연습 안내
- 다음을 선택하는 문을 만드세요:
census테이블의state열census테이블의pop2008열 합계state_fact테이블의census_division_name열
stmt에.select_from()을 추가해state와name열로census와state_fact테이블을 조인하세요.- 문을
state_fact테이블의name열로 그룹화하세요. - 문
stmt_grouped를 실행하여 모든 레코드를 가져오고results로 저장하세요. results객체를 순회하며 각 레코드를 출력한 뒤 Submit Answer 하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Build a statement to select the state, sum of 2008 population and census
# division name: stmt
stmt = select([
____,
func.sum(____),
____
])
# Append select_from to join the census and state_fact tables by the census state and state_fact name columns
stmt_joined = stmt.select_from(
census.join(____, census.columns.____ == state_fact.columns.____)
)
# Append a group by for the state_fact name column
stmt_grouped = stmt_joined.group_by(____)
# Execute the statement and get the results: results
results = connection.execute(____).fetchall()
# Loop over the results object and print each record.
for record in results:
print(record)