USE [Project 2]
Select * from order_details
Select * from pizza_types
Select * from orders
Select * from pizzas
-----QUESTION 1-------Retrieve the total number of orders placed------
Select COUNT(DISTINCT order_id) as Total_Orders
from orders
---Average Number of Orders
Select AVG(quantity) as Average_Number
from order_details
-----QUESTION 2-------Calculate the total revenue generated from pizza sales------
Select CAST(SUM(o.quantity * p.price) AS decimal(10,2)) as 'Total Revenue'
from order_details o
JOIN pizzas p ON o.pizza_id = p.pizza_id
-----QUESTION 3-------Identify the Highest Priced Pizza------
With Cte
AS
(
Select pt.name as 'Name',
round(SUM(p.price),2)as Price,
DENSE_RANK() Over(order by round(SUM(p.price),2) desc) as Rnk
from pizza_types pt
JOIN pizzas p ON pt.pizza_type_id = p.pizza_type_id
Group by pt.name
)
Select name,Price
from Cte
where Rnk = 1
-----QUESTION 4-------Identify the Most common pizza size ordered------
Select p.size,
COUNT(DISTINCT o.order_id) as 'Total Orders',
SUM(o.quantity) as 'Total'
from order_details o
JOIN pizzas p ON o.pizza_id = p.pizza_id
Group by p.size
Order by COUNT(distinct o.order_id) desc
-----QUESTION 5-------List the top 5 most ordered pizza types along with their quantities------
WITH CTE_TOP
AS
(Select pt.name as 'Pizza',
SUM(o.quantity) as 'Quantity',
DENSE_RANK() Over(Order by SUM(o.quantity) desc) as RNK
from order_details o
JOIN pizzas p
ON o.pizza_id = p.pizza_id
JOIN pizza_types pt ON p.pizza_type_id = pt.pizza_type_id
Group by pt.name
)
Select Pizza,Quantity
from CTE_TOP
where RNK IN (1,2,3,4,5)
-----QUESTION 6-------Join the neccessary tables to find the total quantity of each pizza category ordered------
Select pt.category as 'Category',
SUM(o.quantity) as 'Quantity'
from order_details o
JOIN pizzas p
ON o.pizza_id = p.pizza_id
JOIN pizza_types pt ON p.pizza_type_id = pt.pizza_type_id
Group by pt.category
Order by quantity desc
-----QUESTION 7-------------Determine the distribution of orders by hour of the day------
Select DATEPART(HOUR,time) as 'Hour of the Day',
COUNT(order_id) as 'No of Orders'
from orders
Group by DATEPART(HOUR,time)
order by [No of Orders] desc
-----QUESTION 8-------------Find the category-wise distribution of pizzas------
Select category as 'Category',
COUNT(pizza_type_id) as 'No of Orders'
from pizza_types
Group by category
Order by [No of Orders]
-----QUESTION 9-------------Calculate the average number of pizzas ordered per day------
With CTE_AVG
AS
(Select o.date as 'DATE',
SUM(od.quantity) as 'Total Pizza Ordered that Day'
from order_details od
JOIN orders o
ON od.order_id = o.order_id
Group by o.date
)
Select AVG([Total Pizza Ordered that Day]) as AVG_NUMBER_OF_PIZZAS
from CTE_AVG
----SOL 2
Select AVG([Total Pizza Ordered that Day]) as AVG_NUMBER_OF_PIZZAS
from
(Select o.date as 'DATE',
SUM(od.quantity) as 'Total Pizza Ordered that Day'
from order_details od
JOIN orders o
ON od.order_id = o.order_id
Group by o.date
) a
-----QUESTION 10-------------Determine the top 3 most ordered pizza types based on revenue------
Select TOP 3 pt.name as 'Pizza Types',
ROUND(SUM(od.quantity * p.price),2) AS REVENUE
from order_details od
join pizzas p on p.pizza_id = od.pizza_id
join pizza_types pt on pt.pizza_type_id = p.pizza_type_id
Group by pt.name
Order by REVENUE desc
---SOL 2
WITH CTE_TOP3
AS
(Select pt.name as 'Pizza Types',
ROUND(SUM(od.quantity * p.price),2) AS REVENUE
from order_details od
JOIN pizzas p
ON p.pizza_id = od.pizza_id
JOIN pizza_types pt
ON pt.pizza_type_id = p.pizza_type_id
Group by pt.name
)
Select top 3[Pizza Types],
REVENUE
from CTE_TOP3
Order by REVENUE desc
-----ADVANCED 1-------------Calculate the percentage contribution of each pizza type to total revenue------
Select pt.category,
concat(cast((sum(o.quantity*p.price)
/
(select sum(od.quantity*p.price) from order_details od join pizzas p on p.pizza_id = od.pizza_id ))*100 as decimal(10,2)), '%')
as 'Revenue contribution from pizza'
from order_details o
JOIN pizzas p
ON p.pizza_id = o.pizza_id
JOIN pizza_types pt
ON pt.pizza_type_id = p.pizza_type_id
Group by pt.category
order by [Revenue contribution from pizza] desc
-----ADVANCED 2-------------Calculate the percentage revenue contribution from each pizza by pizza name------
Select pt.name as 'Name',
CONCAT(CAST((SUM(od.quantity * p.price)
/
(Select SUM(o.quantity * p.price) from order_details o JOIN pizzas p ON p.pizza_id = o.pizza_id )) * 100 AS decimal(10,2) ),'%')
as 'Revenue contribution from pizza'
from order_details od
JOIN pizzas p
ON p.pizza_id = od.pizza_id
JOIN pizza_types pt
ON pt.pizza_type_id = p.pizza_type_id
Group by pt.name
Order by [Revenue contribution from pizza] desc
-----ADVANCED 3-------------Analyze the cummulative revenue generated over time------
WITH CTE_CUMMULATIVE
AS
(Select o.date as 'DATE',
ROUND(SUM(od.quantity * p.price),2) as REVENUE
from order_details od
JOIN orders o
ON od.order_id = o.order_id
JOIN pizzas p ON od.pizza_id = p.pizza_id
Group by o.date
)
Select DATE,
REVENUE,
ROUND(SUM(REVENUE) OVER(Order by date asc),2) as 'Cummulative Sum'
from CTE_CUMMULATIVE
Group by DATE,REVENUE
-----ADVANCED 4-------------Determine the top 3 most ordered pizza types based on revenue for each pizza category------
WITH CTE
AS
(
Select pt.name as NAME,
pt.category as CATEGORY,
CAST(SUM(od.quantity * p.price) AS decimal(10,2)) as REVENUE
-- DENSE_RANK() OVER(PARTITION by pt.category Order by CAST(SUM(od.quantity * p.price) AS decimal(10,2)) desc) as RNK
from order_details od
JOIN pizzas p
ON od.pizza_id = od.pizza_id
JOIN pizza_types pt ON p.pizza_type_id = pt.pizza_type_id
GROUP BY pt.category,pt.name
)
---Select
-- Name,
--- category,
-- revenue
--from cte
--where RNK IN (1,2,3)
, CTE1 AS
( Select NAME,
CATEGORY,
REVENUE,
DENSE_RANK() OVER(PARTITION by Category Order by Revenue desc) as RNK
from cte
)
Select NAME,
CATEGORY,
REVENUE
from CTE1
where RNK IN (1,2,3)
Order by category,name, revenue