LoslegenKostenlos loslegen

Optimized GROUP BY and ORDER BY

Getting GROUP BY and ORDER BY fields in the proper order is critical for performance. This is even more important when it is SORTKEYs being used in those clauses.

Diese Übung ist Teil des Kurses

Introduction to Redshift

Kurs anzeigen

Anleitung zur Übung

  • Properly pull the year and month from the date as separate columns in the select clause.
  • Add the ORDER BY fields month and year in the proper order in the statement.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

-- Extract the year and month from the date as separate elements
SELECT ___(___, date) as year,
       ___('month', ___) as month,
       COUNT(
           CASE WHEN hourlywindspeed != '0' THEN 1
           ELSE NULL
           END
       )
  FROM public_intro_redshift.coffee_county_weather
 GROUP BY year, month
 -- Add the ORDER BY fields in the proper order
 ORDER BY ___ DESC, ___ DESC;
Code bearbeiten und ausführen