Case Study 1 Solution
Case Study 1 Solution
Case Study 1 Solution
--3) how much money has been spend on marketing for product id 1
select sum(marketing) as total from fact
where ProductId = 1;
--6) Display the Details of the productid where product type is coffee
select
sum(profit) as total_profit
from fact
inner join location on fact.Area_Code = location.Area_Code
where location.state = 'colorado';
--12) Display the average budget margin where average budget margin should be
greater than100
select productid, avg(budget_margin) as avg_budget_margin from fact
group by ProductId
having avg(budget_margin) > 100;
--14) Display the average total expense of each product id on individual date
select
f.date,
f.productid,
p.product_type,
p.product,
f.sales,
l.state,
l.area_code
from fact f inner join product p on f.productid = p.productid
inner join location l on f.area_code = l.area_code;
--16) Display the rank without any gap to show the Sales wise rank.
select sales, dense_rank() over (order by sales asc) as rank from fact;
--19) Find the State wise Profit and Sales along with the Product Name
select
l.state,
sum(f.profit) as profit,
sum(f.sales) as sales,
p.product
from fact f inner join location l on f.area_code = l.area_code
inner join product p on p.productid = f.ProductId
group by l.state, p.product;
--20) Find the maximum profit along with the Product id and Product Type.
select
fact.productid,
max(profit) as max_profit,
product.Product_Type
from fact inner join product on fact.ProductId = Product.ProductId
group by fact.ProductId, Product_Type;
/*21) Create a Stored Procedure to fetch the result
according to the product type fromProduct.*/
CREATE PROCEDURE PTYPE @PROD_TYP VARCHAR(20) AS
SELECT * FROM Product
WHERE Product_Type = @PROD_TYP
/* 23) Give the total weekly sales value with the Date and productid details.
Use roll-up to pull the data in hierarchical order.*/
SELECT DATE, PRODUCTID, SUM(SALES) AS TOTAL_SALES FROM FACT
GROUP BY DATE, PRODUCTID WITH ROLLUP;
ROLLBACK
/*29)Display the ASCII value of the fifth character from the column product.*/
SELECT ASCII(SUBSTRING(Product, 5, 1)) AS ASCII_VALUE FROM PRODUCT;