This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--+----------------------------------------------------------------------------------------------------------------------+-- | |
-- OS200H | |
-- Resource Optimization in Snowflake | |
-- Hands on Lab for Snowflake Summit 2023 | |
-- David A Spezia | |
-- Principal Sales Engineer | |
-- HighTech, TelCo & Media | |
-- david.spezia@snowflake.com | |
-- June 2023 | |
-- SQL: https://docs.google.com/document/d/1v97BH450BBTJu1IUKXThZVuyavdvD7WNeDXG-b8MpJ4/edit?usp=sharing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--+----------------------------------------------------------------------------------------------------------------------+-- | |
-- Dynamic IN() Clause from a Session Variable | |
-- David A Spezia | |
-- Solution Architect | |
-- Sigma Computing | |
-- david.spezia@sigmacomputing.com | |
-- 04 Novemebr 2023 | |
--+----------------------------------------------------------------------------------------------------------------------+-- | |
-- <SQL> | |
--+----------------------------------------------------------------------------------------------------------------------+-- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--view to unwind for analysts | |
create or replace view demo_db.iot.json_hybrid_vw as ( | |
select | |
log_ts as LogDate, | |
v:date::date as Date, | |
v:serial_number::string as SerialNumber, | |
v:model::string as Model, | |
v:capacity_bytes::number as CapacityBytes, | |
v:failure::number Failure, | |
round(CapacityBytes/power(1024,3),2) as Capacity_GB, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--create a hybrid table | |
create or replace table demo_db.iot.json_hybrid ( | |
log_ts timestamp, | |
serial_number string, | |
v variant, | |
path_name string, | |
file_name string | |
) cluster by (date(log_ts) , serial_number); | |
--copy data from stage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//AUTOMATICALLY GENERATED DATA DICTIONARY EXAMPLE JSON INGESTED INTO SNOWFLAKE | |
{ | |
"columns":[ | |
{ | |
"columnId":3, | |
"columnName":"V", | |
"dataType":"FIXED", | |
"distinctValues":1, | |
"minValue":100, | |
"nullCount":0, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--I am using demo_db.iot for my example work | |
create database if not exists demo_db; | |
create schema if not exists demo_db.iot; | |
--file format | |
create or replace file format demo_db.iot.json | |
type = 'json' | |
compression = 'auto' | |
enable_octal = false | |
allow_duplicate = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--County Data with Populations, Deaths and Cases and Utah County Areas Allocated by Population for Tableau TWB | |
WITH | |
cases AS ( | |
WITH counties AS ( | |
SELECT JHU.FIPS, JHU.PROVINCE_STATE, JHU.COUNTY, JHU.DATE, GEOSQL.COUNTY_POPULATION, | |
SUM(JHU.CASES) AS COUNTY_CASES, | |
DIV0(COUNTY_CASES,GEOSQL.COUNTY_POPULATION) AS CASES_PER_CAPITA_COUNTY, | |
CASES_PER_CAPITA_COUNTY * 100000 AS CASES_PER_100K | |
FROM STARSCHEMA_COVID19.PUBLIC.JHU_COVID_19 JHU | |
LEFT JOIN ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--MAKE JOIN AND ALLOCATION SQL IF NULL FOR UTAH | |
WITH counties AS ( | |
SELECT | |
JHU.FIPS, | |
JHU.PROVINCE_STATE, | |
JHU.COUNTY, | |
JHU.DATE, | |
GEOSQL.COUNTY_POPULATION, | |
SUM(JHU.CASES) AS COUNTY_CASES, | |
DIV0(COUNTY_CASES, GEOSQL.COUNTY_POPULATION) AS CASES_PER_CAPITA_COUNTY, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*--------- Utah FIPS Mapping Allocation by Population --------------------------------------------------------- | |
County Name JHU UID Code | |
Bear River 84070015 | |
Central Utah 84070016 | |
Southeast Utah 84070017 | |
Southwest Utah 84070018 | |
TriCounty 84070019 | |
Weber-Morgan 84070020 | |
--https://github.com/CSSEGISandData/COVID-19/issues/3066 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
JHU.FIPS, | |
JHU.PROVINCE_STATE, | |
JHU.COUNTY, | |
JHU.DATE, | |
GEOSQL.COUNTY_POPULATION, | |
SUM(JHU.CASES) AS COUNTY_CASES, | |
DIV0(COUNTY_CASES, GEOSQL.COUNTY_POPULATION) AS CASES_PER_CAPITA_COUNTY, | |
CASES_PER_CAPITA_COUNTY * 100000 AS CASES_PER_100K | |
FROM |
NewerOlder