Question 6:
Which Product Line has the largest percentage Month-Over-Month sales (total dollar amount sold)? (Hint:
Look at the LAG function)
SQL:
Select Productlines.Productline, To_Char(Orderdate, 'YYYY-MM') As Month,
Sum(Orderdetails.Quantityordered*(Orderdetails.Priceeach)) As Total_Sale,
(Sum(Orderdetails.Quantityordered*(Orderdetails.Priceeach)) -
Lag(Sum(Orderdetails.Quantityordered*(Orderdetails.Priceeach))) Over (PARTITION BY
productlines.Productline Order By Productlines.Productline)) /
Lag(Sum(Orderdetails.Quantityordered*(Orderdetails.Priceeach))) Over (PARTITION BY
productlines.Productline Order By Productlines.Productline) *100 As Percent_Change
From Productlines
Join Products On Productlines.Productline = Products.Productline
Join Orderdetails On Products.Productcode = Orderdetails.Productcode
Join Orders On Orders.Ordernumber = Orderdetails.Ordernumber
Group By Productlines.Productline, To_Char(Orderdate, 'YYYY-MM')
Order By (Sum(Orderdetails.Quantityordered*(Orderdetails.Priceeach)) -
Lag(Sum(Orderdetails.Quantityordered*(Orderdetails.Priceeach))) Over (PARTITION BY
productlines.Productline Order By Productlines.Productline)) /
Lag(Sum(Orderdetails.Quantityordered*(Orderdetails.Priceeach))) Over (PARTITION BY
productlines.Productline Order By Productlines.Productline) *100 DESC,
Productlines.Productline, To_Char(Orderdate, 'YYYY-MM');
Results:
Findings:
Motorcycles has the largest percentage Month-Over-Month total sales from the month 2018-09 to 2018-10 of 1927%
increase in total sales. First, I wanted to group the product line along with their month and years. Then I wanted the
total sales of each month/year along with the product line. Then, I found the percent change in the total sales by
utilizing the LAG function along with the Partition By clause to make sure the percent changes won’t override from
one product line to another. Lastly, I ordered it by descending order from largest to least in percent change and found
that motorcycle during 2018-10 had the largest month over month sales of an increase from 2834.1 → 57449. 69 (1927
% increase).