0% found this document useful (0 votes)
22 views5 pages

Case Study 1

Uploaded by

rohit rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Case Study 1

Uploaded by

rohit rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CREATE DATABASE Case_Study1;

USE Case_Study1;

SELECT * FROM Fact;


SELECT * FROM Location;
SELECT * FROM Product;

-- 1. Display the number of states present in the Location Table.

SELECT COUNT(DISTINCT STATE) Ct FROM Location;

-- 2. How many products are of regular type?

SELECT COUNT(ProductId) AS Ct FROM Product


WHERE Type= 'Regular'

-- 3. How much spending has been done on marketing of product ID 1?

SELECT SUM(Marketing) AS Mrktng_SUM


FROM fact
WHERE ProductId = 1;

-- 4. What is the minimum sales of a product?

SELECT Product, Sales FROM PRODUCT AS P


FULL OUTER JOIN
FACT AS F
ON P.ProductId = F.ProductId
WHERE SALES = (SELECT MIN(SALES) FROM fact);

-- 5. Display the max Cost of Good Sold (COGS).

SELECT MAX(COGS) AS Max_COGS FROM fact;

-- 6. Display the details of the product where product type is coffee.

SELECT * FROM Product


WHERE Product_Type = 'Coffee';

-- 7. Display the details where total expenses are greater than 40.

SELECT * FROM FACT


WHERE Total_Expenses > 40;

-- 8. What is the average sales in area code 719?

SELECT Area_Code, AVG(SALES) AS AVG_Sales FROM fact


WHERE Area_Code = 719
GROUP BY Area_Code;

-- 9. Find out the total profit generated by Colorado state.

SELECT SUM(Profit) AS Total_Profit_of_Colorado FROM fact AS F


FULL OUTER JOIN
Location AS L
ON L.Area_Code = F.Area_Code
WHERE State = 'Colorado';

-- 10. Display the average inventory for each product ID

SELECT ProductId, AVG(Inventory) AVG_Inv FROM fact


GROUP BY ProductId
ORDER BY ProductId;

-- 11. Display state in a sequential order in a Location Table

SELECT DISTINCT State FROM Location


ORDER BY State ASC;

-- 12. Display the average budget of the Product where the average budget margin
should be greater than 100.

Select ProductId, AVG(Budget_Margin) as Average_Budget_Margin


from Fact
group by ProductId
having AVG(Budget_Margin) > 100

-- Reference

Select Product, AVG(Budget_Margin) as Average_Budget_Margin from Fact


full outer join
Product
on
Fact.ProductId=Product.ProductId
group by Product
having AVG(Budget_Margin) > 100

-- 13. What is the total sales done on date 2010-01-01?

SELECT SUM(Sales) AS Total_Sales FROM fact


WHERE Date = '2010-01-01';

-- 14. Display the average total expense of each product ID on an individual


date.

SELECT ProductId, Date, AVG(Total_Expenses) AVG_Expenses FROM Fact


GROUP BY Date, ProductId
ORDER BY Date, ProductId

-- 15. Display the table with the following attributes such as date, productID,
product_type, product, sales, profit, state, area_code.

SELECT Date, P.ProductId, Product_Type, Product, Sales, Profit, State,


L.Area_Code FROM Fact AS F
FULL OUTER JOIN
Product AS P
ON F.ProductId = P.ProductId
FULL OUTER JOIN
Location AS L
ON F.Area_Code = L.Area_Code

-- 16. Display the rank without any gap to show the sales wise rank.

SELECT *,
DENSE_RANK() OVER (ORDER BY Sales DESC) AS Sales_wise_Rank
FROM Fact;

-- 17. Find the state wise profit and sales.

SELECT L.State, SUM(Profit) AS Total_Profit, SUM(Sales) AS Total_Sales FROM Fact


as F
FULL OUTER JOIN
Location AS L
ON F.Area_Code = L.Area_Code
GROUP BY State

-- 18. Find the state wise profit and sales along with the product name.

SELECT L.State, P.Product, SUM(Profit) AS Total_Profit, SUM(Sales) AS


Total_Sales FROM Fact as F
FULL OUTER JOIN
Location AS L
ON F.Area_Code = L.Area_Code
FULL OUTER JOIN
Product AS P
ON F.ProductId = P.ProductId
GROUP BY State, Product

-- 19. If there is an increase in sales of 5%, calculate the increasedsales.

CREATE FUNCTION Increased_Sales (@sales FLOAT, @increament FLOAT)


RETURNS INT
AS
BEGIN
RETURN @sales * (1 + @increament)
END;

SELECT *, dbo.Increased_Sales (sales, 0.05) AS New_Sales FROM fact;

-- 20. Find the maximum profit along with the product ID and producttype.

SELECT F.ProductId, Type, MAX(Profit) AS MAX_Profit FROM fact AS F


FULL OUTER JOIN
Product AS P
ON F.ProductId = P.ProductId
GROUP BY F.ProductId, Type
-- ORDER BY ProductId ASC

-- 21. Create a stored procedure to fetch the result according to the product
type from Product Table.

CREATE PROCEDURE PType @Prod_typ varchar(20) as Select * from Product Where


Product_Type = @Prod_typ

Exec PType @Prod_typ = ‘Coffee’ Exec PType @Prod_typ =‘Tea’

-- 22. Write a query by creating a condition in which if the total expenses is


less than 60 then it is a profit or else loss.

SELECT *,
CASE
WHEN Total_Expenses < 60 THEN 'Profit'
ELSE 'Loss'
END AS Profit_or_Loss
FROM fact

SELECT *,
IIF (Total_Expenses < 60, 'Profit', 'Loss') AS Profit_or_Loss
FROM fact;

-- 23. Give the total weekly sales value with the date and product ID details.
Use roll-up to pull the data in hierarchical order.

SELECT DATEPART(WEEK, Date) AS WeekNumber,


ProductId,
SUM(Sales) AS Weekly_Sale
FROM fact
GROUP BY DATEPART(WEEK, Date),ProductId WITH ROLLUP

-- 24. Apply union and intersection operator on the tables which consist of
attribute area code.

Select area_code from fact


union
select area_code from location

select area_code from fact


intersect
select area_code from location

-- 25. Create a user-defined function for the product table to fetch a particular
product type based upon the user’s preference.

Create function Producttable(@product_type varchar(50)) returns table as


return
select * from product
where Product_Type=@product_type

select * from dbo.Producttable(‘Coffee’)

-- 26. Change the product type from coffee to tea where product ID is 1 and undo
it.

select * from product


begin transaction update product set product_type=‘Tea’ where productid =1

select * from product


rollback transaction
select * from product

-- 27. Display the date, product ID and sales where total expenses are between
100 to 200.

SELECT Date, ProductId, Sales FROM fact


WHERE Total_Expenses BETWEEN 100 AND 200

-- 28. Delete the records in the Product Table for regular type.
DELETE Product
WHERE Type = 'Regular'

29. Display the ASCII value of the fifth character from the columnProduct.

You might also like