BYU Student Author: @Andrew
Reviewers: @Jae
Estimated Time to Solve: 30 Minutes
We provide the solution to this challenge using:
- SQL (Azure Data Studio)
Need a program? Click here.
Overview
You are an accountant for a small bicycle company. Recently, you received an urgent email from the company’s sales team. They noticed a backlog of sales orders that have not yet been shipped, and they need a report to understand the scope of the problem.
They have requested a report including the following items:
- A table displaying the number of unshipped orders, the average time between the order and the current date (use 2018-12-29 for the current date) for each store for all unshipped orders.
- A table showing each order ID, order date, store name, customer name, and time between the order and the current date (use 2018-12-29 for the current date) for all unshipped orders. Sort the table by oldest orders to newest orders.
You will deliver this report to the sales manager in an excel file containing the relevant data. Format column names into readable formats for the sales manager.
Instructions
First, follow the instructions below to install Azure Data Studio and then load the database. If you already have Azure Data Studio installed and a local server of Microsoft SQL Server then skip to loading the database.
Azure Installation Instructions
-
Install Microsoft SQL Server (if not already installed) found here. Download the developer version (seen in the image below) and install package. When prompted select the basic installation package.
-
Install Azure Data Studio (if not already installed). Installation instructions can be found here.
-
Open Azure Data Studio and set up a new local server instance (if not already done):
Database Loading Instructions
- Download the database setup file: Challenge69_Database.sql.
- Open it in Azure Data Studio, make sure the selected server is localhost, and run the file. Running the file will create a database named BikeStores, then create tables and insert data into those tables.
For more information on the database structure, tables, and fields see SQLServerTutorial.net’s data dictionary here.
This database, ERD, and data dictionary are all owned by SQLServerTutorial.net and used within this challenge under their terms of use.
Second, review the entity-relationship diagram for the BikeStores database. The ERD will be useful in writing your queries.
Third, setup a new query (CTRL + N shortcut) to complete your data retrieval to build the requested tables and export them to an excel file.
The first table will output 3 rows and columns named:
- Store Name
- Number of Orders
- Avg Days Since Order
Table 1 Hints
The general structure of this query is:
SELECT
- Store Name
- Number of Orders =>
COUNT(order_id)
- Avg Days Since Order =>
AVG(DATEDIFF(day, order_date,'2018-12-29'))
FROM
- Join the sales orders table with the sales stores table on store ID
WHERE
- Include your filter to only capture the orders that have not shipped =>
shipped_date IS NULL
GROUP BY
- Group by store name
ORDER BY
- Order by store name
The second table will output 170 rows and columns named:
- Order ID
- Order Date
- Store
- Customer
- Days Since Order
Table 2 Hints
The general structure of this query is:
SELECT
- Order ID
- Order Date
- Store
- Customer =>
(first_name + ' ' + last_name)
- Days Since Order =>
DATEDIFF(day,order_date,'2018-12-29')
FROM
- Required tables:
- Sales.Orders
- Sales.Customers
- Sales.Stores
WHERE
- Include your filter to only capture the orders that have not shipped =>
shipped_date IS NULL
ORDER BY
- Order by order date
To export tables to excel from Azure Data Studio, click the icon (displayed below) on the right of the result table. You will be prompted to select a location for your table to be saved in a .xlsx file.
Solution
Solution Code
--TABLE 1
SELECT
ss.store_name AS 'Store Name',
COUNT(so.order_id) AS 'Number of Orders',
AVG(DATEDIFF(day,so.order_date,'2018-12-29')) AS 'Avg Days Since Order'
FROM
sales.orders so
JOIN sales.stores ss
ON so.store_id = ss.store_id
WHERE so.shipped_date IS NULL
GROUP BY ss.store_name
ORDER BY ss.store_name;
--
--TABLE 2
SELECT
so.order_id AS 'Order ID',
so.order_date AS 'Order Date',
ss.store_name AS 'Store',
(sc.first_name + ' ' + sc.last_name) AS 'Customer',
DATEDIFF(day,so.order_date,'2018-12-29') AS 'Days Since Order'
FROM
sales.orders so
JOIN sales.customers sc
ON so.customer_id = sc.customer_id
JOIN sales.stores ss
ON so.store_id = ss.store_id
WHERE so.shipped_date IS NULL
ORDER BY so.order_date;
Challenge69_SQL_Solution.sql
Challenge69_Excel_Solution.xlsx
Solution Video: Challenge 69|SQL – Bike Shop Shipping Delays