BaşlayınÜcretsiz başlayın

Join'lerle daha fazla pratik

Son egzersizde oluşturduğun seçme ifadesini yine kullanabilirsin; ancak bu kez küçük bir değişiklik yapıp yalnızca birkaç sütunu döndürelim ve diğer tabloyu bir group_by() ifadesinde kullanalım.

Bu egzersiz, kursun bir parçasıdır

Python ile Veritabanlarına Giriş

Kursa Göz Atın

Egzersiz talimatları

  • Şu alanları seçen bir ifade oluştur:
    • census tablosundan state sütunu.
    • census tablosundan pop2008 sütununun toplamı.
    • state_fact tablosundan census_division_name sütunu.
  • stmt ifadesine .select_from() ekleyerek census ve state_fact tablolarını state ve name sütunları üzerinden birleştir.
  • İfadeyi state_fact tablosunun name sütununa göre grupla.
  • Tüm kayıtları almak için stmt_grouped ifadesini çalıştır ve sonucu results olarak kaydet.
  • results nesnesi üzerinde döngü kurup her kaydı yazdırarak yanıtı gönder.

Uygulamalı etkileşimli egzersiz

Bu egzersizi bu örnek kodu tamamlayarak deneyin.

# 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)
Kodu Düzenle ve Çalıştır