Business Case - Target SQL
Business Case - Target SQL
Contents
SELECT
column_name as `Field Name`,
data_type as `Type`
FROM `target.INFORMATION_SCHEMA.COLUMNS`
WHERE table_name = 'customers'
GROUP BY
`Field Name`, `Type`;
SELECT
COUNT(DISTINCT customer_id) as customer_id,
COUNT(DISTINCT customer_unique_id) as customer_unique_id,
COUNT(DISTINCT customer_zip_code_prefix) as customer_zip_code_prefix,
COUNT(DISTINCT customer_city) as customer_city,
COUNT(DISTINCT customer_state) as customer_state,
FROM `targetsql-1223.target.customers`;
customer_unique customer_cit
customer_id _id customer_zip_code_prefix y customer_state
99441 96096 14994 4119 27
B. Get the time range between which the orders were placed
FirstOrder LastOrder
SELECT
customer_state,
customer_city,
COUNT(customer_id) as customer_count
FROM `targetsql-1223.target.customers`
GROUP BY customer_state, customer_city;
To get the total count of distinct customer cities and customer states
SELECT
COUNT( DISTINCT customer_state ) as Distinct_states
FROM `targetsql-1223.target.customers`;
SELECT
COUNT( DISTINCT customer_city ) as Distinct_cities
FROM `targetsql-1223.target.customers`;
II. In-depth Exploration
_________________________________________________________________________
WITH ordercount AS (
SELECT
EXTRACT(YEAR FROM order_purchase_timestamp) as year,
EXTRACT(MONTH FROM order_purchase_timestamp) as month,
COUNT(order_id) as order_count
FROM `targetsql-1223.target.orders`
GROUP BY
EXTRACT(YEAR FROM order_purchase_timestamp),
EXTRACT(MONTH FROM order_purchase_timestamp)
)
WITH hourly_ordercounts AS (
SELECT
EXTRACT(HOUR FROM order_purchase_timestamp) AS hour_of_day,
COUNT(order_id) AS order_count
FROM `targetsql-1223.target.orders`
GROUP BY EXTRACT(HOUR FROM order_purchase_timestamp)
)
SELECT
*,
CASE
WHEN hour_of_day < 6 AND hour_of_day >= 4 THEN 'DAWN'
WHEN hour_of_day < 12 AND hour_of_day >= 6 THEN 'MORNING'
WHEN hour_of_day < 18 AND hour_of_day >= 12 THEN 'AFTERNOON'
ELSE 'NIGHT'
END AS period
FROM hourly_ordercounts
ORDER BY order_count DESC;
The provided query helps in analyzing the hourly order count and identifying peak hours of orders
by grouping them based on the period of the day (`DAWN`, `MORNING`, `AFTERNOON`, or
`NIGHT`). This allows you to determine which specific periods experience the highest order
activity. The result is sorted in descending order of total order count, making it easy to identify the
most active periods.
WITH hourly_ordercounts AS (
SELECT
EXTRACT(HOUR FROM order_purchase_timestamp) AS hour_of_day,
COUNT(order_id) AS order_count
FROM `targetsql-1223.target.orders`
GROUP BY EXTRACT(HOUR FROM order_purchase_timestamp)
)
SELECT
period,
SUM(order_count) AS total_order_count
FROM (
SELECT
*,
CASE
WHEN hour_of_day < 6 AND hour_of_day >= 4 THEN 'DAWN'
WHEN hour_of_day < 12 AND hour_of_day >= 6 THEN 'MORNING'
WHEN hour_of_day < 18 AND hour_of_day >= 12 THEN 'AFTERNOON'
ELSE 'NIGHT'
END AS period
FROM hourly_ordercounts
) periods
GROUP BY period
ORDER BY total_order_count DESC;
The maximum order count occurs around 4:00 PM, but the total count of orders
is highest during the nighttime period, as observed from the results of the two
queries above
_________________________________________________________________________
WITH order_summary AS (
SELECT
cust.customer_state as STATE,
EXTRACT(YEAR FROM order_purchase_timestamp) as year,
EXTRACT(MONTH FROM order_purchase_timestamp) as month,
COUNT(order_id) AS total_orders
FROM `targetsql-1223.target.orders` ord
LEFT JOIN `targetsql-1223.target.customers` cust ON cust.customer_id =
ord.customer_id
GROUP BY cust.customer_state, EXTRACT(YEAR FROM order_purchase_timestamp),
EXTRACT(MONTH FROM order_purchase_timestamp)
)
SELECT * FROM order_summary
ORDER BY order_summary.year, order_summary.month, order_summary.STATE;
SELECT
COUNT(DISTINCT customer_id) as total_customers ,
customer_state
FROM `targetsql-1223.target.customers`
GROUP BY customer_state
ORDER BY total_customers DESC;