SQL Test Passed
SQL Test Passed
Q1: Show list of transactions occurring in February 2018 with SHIPPED status
SELECT *
FROM test.transactions
WHERE
MONTH(transaction_date) = 2 AND
YEAR(transaction_date) = 2018;
SELECT *
FROM test.transactions
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
ON
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
ON
Q5: Count the transactions from each vendor with the status CANCELLED per day
FROM test.transactions
Q6: Show a list of customers who made more than 1 SHIPPED purchases
SELECT customer_id
FROM test.transactions
GROUP BY customer_id
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
CASE
FROM test.transactions)
SELECT
vendor,
CASE
WHEN
SUM(cancelled) = 0
THEN 'Superb'
WHEN
SUM(cancelled) >= 1
THEN 'Good'
ELSE 'Normal'
END AS category
FROM vendor_status
GROUP BY vendor
ORDER BY
CASE
ELSE 3
END ASC,
total_transaction DESC;
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
CASE
CASE
WHEN status = "PROCESSING" THEN 1
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,
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
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
*,
FROM test.transaction_details)
SELECT
product_name,
FORMAT(SUM(value),0,'de_DE') AS value_price_x_quantity,
CASE
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
ON td_indomie.trx_id = t.id
WHERE
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
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
ON
t.transaction_date = l.latest_date;