0% found this document useful (0 votes)
10 views10 pages

Business Case - Target SQL

The document outlines a business case for analyzing e-commerce data in Brazil, focusing on exploratory data analysis, trends in orders, and customer buying patterns. It includes sections on the evolution of e-commerce orders, economic impacts, and analyses based on sales, freight, delivery times, and payment methods. Key findings include a growing trend in order counts and insights into customer preferences for purchasing times.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Business Case - Target SQL

The document outlines a business case for analyzing e-commerce data in Brazil, focusing on exploratory data analysis, trends in orders, and customer buying patterns. It includes sections on the evolution of e-commerce orders, economic impacts, and analyses based on sales, freight, delivery times, and payment methods. Key findings include a growing trend in order counts and insights into customer preferences for purchasing times.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Business Case: Target SQL

Contents

I. Basic EDA (Exploratory Data Analysis)


- A. Data Type of Columns in Dataset
- B. Time Range of Orders
- C. Cities & States of Customers
___________________________________________________________________________

II. In-depth Exploration


- A. Growing Trend in Number of Orders
- B. Monthly Seasonality in Number of Orders
- C. Buying Patterns in Brazil: When do Customers Prefer to Make
Purchases?
___________________________________________________________________________

III. Evolution of E-commerce Orders in Brazil


- A. Monthly Orders in Each State
- B. Distribution of Customers Across States
___________________________________________________________________________

IV. Impact on Economy: Analyzing Money Movement


- A. % Increase in Cost of Orders (2017-2018)
- B. Total & Average Order Price by State
- C. Total & Average Order Freight by State
___________________________________________________________________________

V. Analysis Based on Sales, Freight, and Delivery Time


- A. Delivery Time and Estimated vs Actual Delivery Date
- B. Top 5 States by Highest & Lowest Average Freight
- C. Top 5 States by Highest & Lowest Average Delivery Time
- D. Top 5 States with Fastest Delivery Compared to Estimated Date
___________________________________________________________________________

VI. Analysis Based on Payments


- A. Monthly Orders by Payment Types
- B. Orders Based on Paid Payment Installments
___________________________________________________________________
I. Basic EDA (Exploratory Data Analysis)
_________________________________________________________________________

A. Customer Table Data Types:

● Show data types for each column in the "customers" table.

Total Number of Rows in customers table : 99441

SELECT COUNT(*) as total_number_of_rows FROM `targetsql-1223.target.customers`;

Data type of all columns in the “customers” table.

SELECT
column_name as `Field Name`,
data_type as `Type`
FROM `target.INFORMATION_SCHEMA.COLUMNS`
WHERE table_name = 'customers'
GROUP BY
`Field Name`, `Type`;

Field Name Type


customer_id STRING
customer_unique_id STRING
customer_zip_code_prefix INT64
customer_city STRING
customer_state STRING

Number of Unique Items in Each Column

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

The Primary Key for the “customers” table is customer_id

B. Get the time range between which the orders were placed

SELECT min(order_purchase_timestamp) as FirstOrder,


max(order_purchase_timestamp) as LastOrder
FROM `targetsql-1223.target.orders`;

FirstOrder LastOrder

2016-09-04 21:15:19.000000 UTC 2018-10-17 17:30:18.000000 UTC

C. Cities & States of Customers

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
_________________________________________________________________________

A. Growing Trend in Number of Orders

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)
)

SELECT * FROM ordercount ORDER BY year ,month;


The above query grouped 'ordercount' by years and months. It can be observed that in the second
half of November 2017 , there is an increase in order count. This reflects a growing trend in
e-commerce.

C. Buying Patterns in Brazil: When do Customers Prefer to Make


Purchases?

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

III. Evolution of E-commerce Orders in Brazil

_________________________________________________________________________

A. Monthly Orders in Each State

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;

B. Distribution of Customers Across States

SELECT
COUNT(DISTINCT customer_id) as total_customers ,
customer_state
FROM `targetsql-1223.target.customers`
GROUP BY customer_state
ORDER BY total_customers DESC;

You might also like