Email : sherlytaurinsiri@gmail.com
Instagram : https://www.instagram.com/sherlytaurin
RPubs : https://rpubs.com/sherlytaurin/
Github : https://github.com/sherlytaurin/
Telegram : @Sherlytaurin
Persiapan Data
Load Packages
Connect ke MySQL
sherly <- dbConnect(MySQL(),
user = 'root',
password = '',
dbname = 'factory_db',
host = 'localhost',
port = 3306)
knitr::opts_chunk$set(connection = "sherly") # mengatur connection agar bisa menggunakan SQL ChunkSELECT C.CustomerName, S.SupplierName, C.Country AS 'CustomerCountry', S.Country AS 'SupplierCountry'
FROM customers C
JOIN Orders O
ON C.CustomerID=O.CustomerID
JOIN OrderDetails OD
ON O.OrderID=OD.OrderID
JOIN Products P
ON OD.ProductID=P.ProductID
JOIN Suppliers S
ON P.SupplierID=S.SupplierID
WHERE
CASE
WHEN C.Country=S.Country THEN '1'
ELSE '0'
END = '1';
Because there is no fax number column in the database, we assume that all of the suppliers don’t have any fax number.
SELECT CustomerName, AVG(TotalOrderAmount) AS AverageOrder
FROM
(
SELECT CustomerName, SUM(OD.Quantity*P.Price) AS TotalOrderAmount
FROM customers C
JOIN orders O
ON C.CustomerID = O.CustomerID
JOIN orderdetails OD
ON O.OrderID = OD.OrderID
JOIN products P
ON OD.ProductID = P.ProductID
GROUP BY OD.OrderID
) S
GROUP BY S.CustomerName
HAVING AverageOrder
BETWEEN 1000 AND 1200
ORDER BY AverageOrder ASC;