Fonctions, tri et regroupement

Introduction à Snowflake SQL

George Boorman

Senior Curriculum Manager, DataCamp

Fonctions de chaîne – INITCAP

Syntaxe : INITCAP( <expr> )

  • Met la première lettre de chaque mot en majuscule
SELECT INITCAP(category) AS capitalized_category 
FROM pizza_type

Résultat : noms de pizzas en majuscules initiales

Introduction à Snowflake SQL

Fonctions de chaîne – CONCAT

  • Combine les expressions

Syntaxe :

CONCAT( <expr1> [ , <exprN> ... ] )

Avant CONCAT :

Colonne category avant concaténation

  • Combiner category avec ' - Pizza'
    SELECT CONCAT(category, ' - Pizza') 
      AS pizza_category 
    FROM pizza_type
    

Après CONCAT :

Colonne category après concaténation

Introduction à Snowflake SQL

Fonctions DATE et TIME

  • CURRENT_DATE() ou CURRENT_DATE
  • CURRENT_TIME() ou CURRENT_TIME
SELECT CURRENT_DATE
SELECT CURRENT_TIME

Résultat : date courante

Résultat : heure courante

Introduction à Snowflake SQL

EXTRACT

Syntaxe

  • EXTRACT( <date_or_time_part> FROM <date_or_time_expr> )
    • <date_or_time_part>year, month, day, weekday, etc.
SELECT EXTRACT(MONTH FROM order_date) AS order_month,
    COUNT(*) AS num_orders
FROM orders
GROUP BY order_month

Nombre de commandes par mois

Introduction à Snowflake SQL

TRI et REGROUPEMENT

  • TRI : ORDER BY
  • REGROUPEMENT : GROUP BY
    • Snowflake : GROUP BY ALL
Introduction à Snowflake SQL

GROUP BY ALL

  • GROUP BY column1, column2
SELECT 
    pizza_type_id,
    size,
    AVG(price) AS average_price
FROM 
    pizzas
GROUP BY
    pizza_type_id, -- explicit columns
    size
ORDER BY 
    pizza_type_id, average_price DESC

  • GROUP BY ALL

 

SELECT 
    pizza_type_id,
    size,
    AVG(price) AS average_price
FROM 
    pizzas
GROUP BY ALL -- Ne pas spécifier les colonnes 
ORDER BY 
    pizza_type_id, average_price DESC
Introduction à Snowflake SQL

Résumé

Fonction/mot-clé Utilisation
INITCAP() Mettre en majuscule chaque mot d'une chaîne
CONCAT() Combiner plusieurs chaînes
CURRENT_DATE Obtenir la date courante
CURRENT_TIME Obtenir l'heure courante
EXTRACT Extraire un élément date/heure, p. ex. month d'une date
ORDER BY Trier les résultats de la requête
GROUP BY ALL Regrouper par toutes les colonnes (non agrégées)
Introduction à Snowflake SQL

Passons à la pratique !

Introduction à Snowflake SQL

Preparing Video For Download...