Functies, sorteren en groeperen

Introductie tot Snowflake SQL

George Boorman

Senior Curriculum Manager, DataCamp

Stringfuncties - INITCAP

Syntax: INITCAP( <expr> )

  • Elk woord in een string met hoofdletter
SELECT INITCAP(category) AS capitalized_category 
FROM pizza_type

GeKapitaliseerde pizzanamen resultaat

Introductie tot Snowflake SQL

Stringfuncties - CONCAT

  • Voegt de expressies samen

Syntax:

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

Voor concat:

Kolom category vóór samenvoegen

  • category combineren met ' - Pizza'
    SELECT CONCAT(category, ' - Pizza') 
      AS pizza_category 
    FROM pizza_type
    

Na concat:

Kolom category na samenvoegen

Introductie tot Snowflake SQL

DATE & TIME-functies

  • CURRENT_DATE() of CURRENT_DATE
  • CURRENT_TIME() of CURRENT_TIME
SELECT CURRENT_DATE
SELECT CURRENT_TIME

Huidige datum resultaat

Huidige tijd resultaat

Introductie tot Snowflake SQL

EXTRACT

Syntax

  • 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

Aantal bestellingen per maand

Introductie tot Snowflake SQL

SORTEREN en GROEPEREN

  • SORTEREN: ORDER BY
  • GROEPEREN: GROUP BY
    • Snowflake: GROUP BY ALL
Introductie tot 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 -- Don't specify columns 
ORDER BY 
    pizza_type_id, average_price DESC
Introductie tot Snowflake SQL

Samenvatting

Functie/trefwoord Gebruik
INITCAP() Elk woord in een string met hoofdletter
CONCAT() Meerdere strings samenvoegen
CURRENT_DATE Huidige datum ophalen
CURRENT_TIME Huidige tijd ophalen
EXTRACT Een datum/tijddeel halen, bv. month uit een datum
ORDER BY Queryresultaten sorteren
GROUP BY ALL Groeperen op alle (niet-geaggregeerde) kolommen
Introductie tot Snowflake SQL

Laten we oefenen!

Introductie tot Snowflake SQL

Preparing Video For Download...