-
Notifications
You must be signed in to change notification settings - Fork 353
/
Creating Date-Partitioned Tables in BigQuery
107 lines (86 loc) · 2.25 KB
/
Creating Date-Partitioned Tables in BigQuery
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
bq mk ecommerce
bq query --use_legacy_sql=false \
"
#standardSQL
SELECT DISTINCT
fullVisitorId,
date,
city,
pageTitle
FROM \`data-to-insights.ecommerce.all_sessions_raw\`
WHERE date = '20170708'
LIMIT 5
"
bq query --use_legacy_sql=false \
"
#standardSQL
SELECT DISTINCT
fullVisitorId,
date,
city,
pageTitle
FROM \`data-to-insights.ecommerce.all_sessions_raw\`
WHERE date = '20180708'
LIMIT 5
"
bq query --use_legacy_sql=false \
"
#standardSQL
CREATE OR REPLACE TABLE ecommerce.partition_by_day
PARTITION BY date_formatted
OPTIONS(
description='a table partitioned by date'
) AS
SELECT DISTINCT
PARSE_DATE('%Y%m%d', date) AS date_formatted,
fullvisitorId
FROM \`data-to-insights.ecommerce.all_sessions_raw\`
"
bq query --use_legacy_sql=false \
"
#standardSQL
SELECT *
FROM \`data-to-insights.ecommerce.partition_by_day\`
WHERE date_formatted = '2016-08-01'
"
bq query --use_legacy_sql=false \
"
#standardSQL
SELECT *
FROM \`data-to-insights.ecommerce.partition_by_day\`
WHERE date_formatted = '2018-07-08'
"
bq query --use_legacy_sql=false \
"
#standardSQL
SELECT
DATE(CAST(year AS INT64), CAST(mo AS INT64), CAST(da AS INT64)) AS date,
(SELECT ANY_VALUE(name) FROM \`bigquery-public-data.noaa_gsod.stations\` AS stations
WHERE stations.usaf = stn) AS station_name, -- Stations may have multiple names
prcp
FROM \`bigquery-public-data.noaa_gsod.gsod*\` AS weather
WHERE prcp < 99.9 -- Filter unknown values
AND prcp > 0 -- Filter stations/days with no precipitation
AND _TABLE_SUFFIX >= '2018'
ORDER BY date DESC -- Where has it rained/snowed recently
LIMIT 10
"
bq query --use_legacy_sql=false \
"
#standardSQL
CREATE OR REPLACE TABLE ecommerce.days_with_rain
PARTITION BY date
OPTIONS (
partition_expiration_days=60,
description='weather stations with precipitation, partitioned by day'
) AS
SELECT
DATE(CAST(year AS INT64), CAST(mo AS INT64), CAST(da AS INT64)) AS date,
(SELECT ANY_VALUE(name) FROM \`bigquery-public-data.noaa_gsod.stations\` AS stations
WHERE stations.usaf = stn) AS station_name, -- Stations may have multiple names
prcp
FROM \`bigquery-public-data.noaa_gsod.gsod*\` AS weather
WHERE prcp < 99.9 -- Filter unknown values
AND prcp > 0 -- Filter
AND _TABLE_SUFFIX >= '2018'
"