Nama : Nadila Pramesti Regita C
NIM : 41816120085
UAS PL/SQL
Part 1
1. Show a list of customer who made more than 1 SHIPPED purchases
SELECT customer_id, COUNT (*)
FROM transactions
GROUP BY customer_id
HAVING COUNT (*) > 1;
2. 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.
SELECT vendor,
CASE
WHEN SUM(CASE when STATUS = 'SHIPPED' then 1 ELSE 0 END) > 2 AND SUM(CASE when
STATUS = 'CANCELLED' then 1 ELSE 0 END) = 0 THEN 'Superb'
WHEN SUM(CASE when STATUS = 'SHIPPED' then 1 ELSE 0 END) > 2 AND SUM(CASE when
STATUS = 'CANCELLED' then 1 ELSE 0 END) >= 1 THEN 'Good'
ELSE 'Normal'
END AS Category,
count(distinct order_id) AS TotalTransaction
FROM transactions
group by vendor;
3. Group the transactions by hour of transaction_date
SELECT EXTRACT(HOUR FROM transaction_date) "HourOfTheDay", COUNT(order_id)
"TotalTransaction" FROM transactions GROUP BY EXTRACT(HOUR FROM transaction_date)
order by "HourOfTheDay";
4. Group the transactions by day and statuses as the example below
SELECT to_char(transaction_date,'YYYY-MM-DD') as "DATE",
SUM(CASE when STATUS = 'SHIPPED' then 1 ELSE 0 END) as "SHIPPED",
SUM(CASE when STATUS = 'CANCELLED' then 1 ELSE 0 END) as "CANCELLED",
SUM(CASE when STATUS = 'PROCESSING' then 1 ELSE 0 END) as "PROCESSING"
FROM transactions
where STATUS IN ('SHIPPED','CANCELLED','PROCESSING')
GROUP BY to_char(transaction_date,'YYYY-MM-DD') order by
to_char(transaction_date,'YYYY-MM-DD') ;
5. Calculate the average, minimum and maximum of days interval of each transaction (how
many days from one transaction to the next)
select avg(daydiff) as avg_day, min(daydiff) min_day, max(daydiff) max_day from (select
[Link],[Link], trunc(b.transaction_date) - trunc(a.transaction_date) as daydiff
from TRANSACTIONS a
join TRANSACTIONS b on [Link]+1=[Link]);
Part 2
1. 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)
SELECT product_name, SUM (quantity * price) VALUE,
CASE
WHEN SUM (quantity) > 100
THEN SUM (quantity * price) * 4 / 100
ELSE SUM (quantity * price) * 2 / 100
END commission
FROM transaction_details
GROUP BY product_name
2. Show total quantity of “Indomie (all variant)” shipped within February 2018
SELECT SUM(quantity) AS total_quantity FROM transaction_details WHERE product_name
LIKE '%Indomie%'
3. For each product, show the ID of the last transaction which contained that particular
product
SELECT distinct(Product_name), MAX(trx_id) AS LastTransactionId FROM transaction_details
GROUP BY product_name