Skip to content

Commit

Permalink
Create Creating a Data Warehouse Through Joins and Unions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayush7-BIT authored Jul 19, 2023
1 parent 0f2687e commit e2ef287
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions Creating a Data Warehouse Through Joins and Unions
Original file line number Diff line number Diff line change
@@ -0,0 +1,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'"

0 comments on commit e2ef287

Please sign in to comment.