PART 1:
Q1: Show list of transactions occurring in February 2018 with SHIPPED status
SELECT *
FROM test.transactions
WHERE
status = 'SHIPPED' AND
MONTH(transaction_date) = 2 AND
YEAR(transaction_date) = 2018;
Q2: Show list of transactions occurring from midnight to 9 AM
SELECT *
FROM test.transactions
WHERE TIME(transaction_date) BETWEEN "00:00:00" AND "09:00:00";
Q3: Show a list of only the last transactions from each vendor
WITH latest_transaction AS (
SELECT
vendor,
MAX(transaction_date) AS latest_date
FROM test.transactions
GROUP BY vendor
SELECT d.*
FROM test.transactions AS d
INNER JOIN latest_transaction AS l
ON
d.vendor = l.vendor AND
d.transaction_date = l.latest_date;
Q4: Show a list of only the second last transactions from each vendor
WITH second_latest AS (
SELECT
DISTINCT(vendor),
NTH_VALUE(transaction_date,2) OVER(PARTITION BY vendor ORDER BY transaction_date DESC)
AS second_latest_date
FROM test.transactions
SELECT t.*
FROM test.transactions AS t
INNER JOIN second_latest AS s
ON
t.vendor = s.vendor AND
t.transaction_date = s.second_latest_date;
Q5: Count the transactions from each vendor with the status CANCELLED per day
SELECT vendor, DATE(transaction_date), COUNT(*)
FROM test.transactions
WHERE status = "CANCELLED"
GROUP BY vendor, DATE(transaction_date);
Q6: Show a list of customers who made more than 1 SHIPPED purchases
SELECT customer_id
FROM test.transactions
WHERE status = "SHIPPED"
GROUP BY customer_id
HAVING COUNT(*) > 1;
Q7: Show the total transactions (volume) and category of each vendors by following these
criteria:
a. Superb: More than 2 SHIPPED and 0 CANCELLED transactions
b. Good: More than 2 SHIPPED and 1 or more CANCELLED transactions
c. Normal: other than Superb and Good criteria
Order the vendors by the best category (Superb, Good, Normal), then by the biggest
transaction volume
WITH vendor_status AS
(SELECT
vendor,
CASE
WHEN status = "SHIPPED" THEN 1
ELSE 0 END AS shipped,
CASE
WHEN status = "CANCELLED" THEN 1
ELSE 0 END AS cancelled
FROM test.transactions)
SELECT
vendor,
SUM(shipped) + SUM(cancelled) AS total_transaction,
CASE
WHEN
SUM(shipped) > 2 AND
SUM(cancelled) = 0
THEN 'Superb'
WHEN
SUM(shipped) > 2 AND
SUM(cancelled) >= 1
THEN 'Good'
ELSE 'Normal'
END AS category
FROM vendor_status
GROUP BY vendor
ORDER BY
CASE
WHEN category = 'Superb' THEN 1
WHEN category = 'Good' THEN 2
ELSE 3
END ASC,
total_transaction DESC;
Q8: Group the transactions by hour of transaction_date
SELECT
HOUR(TIME(transaction_date)) AS hour_of_the_day,
COUNT(*) AS total_transaction
FROM test.transactions
GROUP BY HOUR(TIME(transaction_date));
Q9: Group the transactions by day and statuses as the example below
WITH date_status AS
(SELECT
DATE(transaction_date) AS Date,
CASE
WHEN status = "SHIPPED" THEN 1
ELSE 0 END AS SHIPPED,
CASE
WHEN status = "CANCELLED" THEN 1
ELSE 0 END AS CANCELLED,
CASE
WHEN status = "PROCESSING" THEN 1
ELSE 0 END AS PROCESSING
FROM test.transactions)
SELECT
Date,
SUM(SHIPPED) AS SHIPPED,
SUM(CANCELLED) AS CANCELLED,
SUM(PROCESSING) AS PROCESSING
FROM date_status
GROUP BY Date;
Q10: Calculate the average, minimum and maximum of days interval of each transaction
(how many days from one transaction to the next)
WITH d_interval AS
SELECT
DATE(transaction_date) AS transaction_date_formated,
IFNULL(LAG(DATE(transaction_date),1) OVER(ORDER BY transaction_date ASC),
DATE(transaction_date)) AS previous_transaction_date,
DATEDIFF(DATE(transaction_date),IFNULL(LAG(DATE(transaction_date),1) OVER(ORDER BY
transaction_date ASC), DATE(transaction_date))) AS days_interval
FROM test.transactions)
SELECT
CONCAT(CAST(AVG(d.days_interval) AS CHAR),' day(s)') AS Average_interval,
CONCAT(CAST(MIN(d.days_interval) AS CHAR),' day(s)') AS Minimum_interval,
CONCAT(CAST(MAX(d.days_interval) AS CHAR),' day(s)') AS Maximum_interval
FROM d_interval AS d;
PART 2:
Q1: Show the sum of the total value of the products shipped along with the Distributor
Commissions (2% of the total product value if total quantity is 100 or less, 4% of the total
product value if total quantity sold is more than 100)
WITH sales_and_commision AS
(SELECT
*,
quantity * price AS value
FROM test.transaction_details)
SELECT
product_name,
FORMAT(SUM(value),0,'de_DE') AS value_price_x_quantity,
CASE
WHEN SUM(quantity) <= 100 THEN FORMAT(SUM(value) * 0.02 ,0,'de_DE')
ELSE FORMAT(SUM(value) * 0.04 ,0,'de_DE') END AS distributor_commision
FROM sales_and_commision
GROUP BY product_name;
Q2: Show total quantity of “Indomie (all variant)” shipped within February 2018
WITH transaction_details_indomie AS
(SELECT *
FROM test.transaction_details
WHERE product_name LIKE 'Indomie%')
SELECT SUM(quantity) AS total_quantity
FROM transaction_details_indomie AS td_indomie
INNER JOIN test.transactions AS t
ON td_indomie.trx_id = t.id
WHERE
status = 'SHIPPED' AND
MONTH(transaction_date) = 2 AND
YEAR(transaction_date) = 2018;
Q3: For each product, show the ID of the last transaction which contained that particular
product
WITH transaction_details_with_date AS
(SELECT
td.*,
t.transaction_date
FROM test.transaction_details AS td
LEFT JOIN test.transactions AS t
ON td.trx_id = t.id),
latest_date_each_product AS
(SELECT
product_name,
MAX(transaction_date) AS latest_date
FROM transaction_details_with_date
GROUP BY product_name)
SELECT
l.product_name,
t.trx_id AS last_transaction_id
FROM transaction_details_with_date AS t
INNER JOIN latest_date_each_product AS l
ON
t.product_name = l.product_name AND
t.transaction_date = l.latest_date;