forked from lavishsheth/code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Creating a Data Warehouse Through Joins and Unions
103 lines (103 loc) · 2.69 KB
/
Creating a Data Warehouse Through Joins and Unions
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
bq mk ecommerce
bq query --use_legacy_sql=false \
'create or replace TABLE ecommerce.products AS
SELECT
*
FROM
`data-to-insights.ecommerce.products`'
bq query --use_legacy_sql=false \
'SELECT
SKU,
name,
sentimentScore,
sentimentMagnitude
FROM
`data-to-insights.ecommerce.products`
ORDER BY
sentimentScore DESC
LIMIT 5'
bq query --use_legacy_sql=false \
'SELECT
SKU,
name,
sentimentScore,
sentimentMagnitude
FROM
`data-to-insights.ecommerce.products`
WHERE sentimentScore IS NOT NULL
ORDER BY
sentimentScore
LIMIT 5'
bq query --use_legacy_sql=false \
"CREATE OR REPLACE TABLE ecommerce.sales_by_sku_20170801 AS
SELECT
productSKU,
SUM(IFNULL(productQuantity, 0)) AS total_ordered
FROM
\`data-to-insights.ecommerce.all_sessions_raw\`
WHERE
date = '20170801'
GROUP BY
productSKU
ORDER BY
total_ordered DESC"
bq query --use_legacy_sql=false \
'CREATE TABLE IF NOT EXISTS ecommerce.sales_by_sku_20170801 AS
SELECT
productSKU,
SUM(IFNULL(productQuantity,0)) AS total_ordered
FROM
`data-to-insights.ecommerce.all_sessions_raw`
WHERE date = "20170801"
GROUP BY productSKU
ORDER BY total_ordered DESC'
bq query --use_legacy_sql=false \
'# join against product inventory to get name
SELECT DISTINCT
website.productSKU,
website.total_ordered,
inventory.name,
inventory.stockLevel,
inventory.restockingLeadTime,
inventory.sentimentScore,
inventory.sentimentMagnitude
FROM
ecommerce.sales_by_sku_20170801 AS website
LEFT JOIN `data-to-insights.ecommerce.products` AS inventory
ON website.productSKU = inventory.SKU
ORDER BY total_ordered DESC'
bq query --use_legacy_sql=false \
'# calculate ratio and filter
SELECT DISTINCT
website.productSKU,
website.total_ordered,
inventory.name,
inventory.stockLevel,
inventory.restockingLeadTime,
inventory.sentimentScore,
inventory.sentimentMagnitude,
SAFE_DIVIDE(website.total_ordered, inventory.stockLevel) AS ratio
FROM
ecommerce.sales_by_sku_20170801 AS website
LEFT JOIN `data-to-insights.ecommerce.products` AS inventory
ON website.productSKU = inventory.SKU
# gone through more than 50% of inventory for the month
WHERE SAFE_DIVIDE(website.total_ordered,inventory.stockLevel) >= .50
ORDER BY total_ordered DESC'
bq query --use_legacy_sql=false \
'CREATE OR REPLACE TABLE ecommerce.sales_by_sku_20170802
(
productSKU STRING,
total_ordered INT64
);'
bq query --use_legacy_sql=false \
"INSERT INTO ecommerce.sales_by_sku_20170802
(productSKU, total_ordered)
VALUES('GGOEGHPA002910', 101)"
bq query --use_legacy_sql=false \
'SELECT * FROM ecommerce.sales_by_sku_20170801
UNION ALL
SELECT * FROM ecommerce.sales_by_sku_20170802'
bq query --use_legacy_sql=false \
"SELECT * FROM \`ecommerce.sales_by_sku_2017*\`
WHERE _TABLE_SUFFIX = '0802'"