Skip to content

Instantly share code, notes, and snippets.

@Luke-SNAW
Last active November 3, 2021 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luke-SNAW/b700050f18e8e10fdb6bdc25234e8226 to your computer and use it in GitHub Desktop.
Save Luke-SNAW/b700050f18e8e10fdb6bdc25234e8226 to your computer and use it in GitHub Desktop.
[DB__SQL] #articles
  • 6 SQL Queries Every Data Engineer Should Be Aware of
    • Running Totals
      SELECT id,month
       , Amount
       , SUM(Amount) OVER (ORDER BY id) as total_sum
      FROM bill
    • Common Table Expressions
      SELECT *
      FROM bill
      WHERE id in 
        (SELECT DISTINCT id 
         FROM id 
         WHERE country = "US"
         AND status = "Y"
        )
      ->
      WITH idtempp as (
        SELECT id as id
        FROM id 
        WHERE country = "US"
        AND status = "Y"
      )
      
      SELECT *
      FROM bill
      WHERE id in (SELECT id from idtempp)
    • Temporary Functions
      CREATE TEMPORARY FUNCTION get_gender(type varchar) AS (
         CASE WHEN type = "M" THEN "male"
              WHEN type = "F" THEN "female"
              ELSE "n/a"
         END
      )
      SELECT 
        name,
        get_gender(Type) as gender
      FROM bill
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment