0% found this document useful (0 votes)
11 views

SqlServer

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

SqlServer

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

Use Casestudy1;

Select * from Product;


Select * from fact;
Select * from location;

---- Display the number of states present in the LocationTable.


select COUNT(distinct state)as StateCount from Location;

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

select count(type) as Count_Grouptype,type from product group by type having


type='Regular';
select COUNT(PRODUCT) as Count_Type from product where type='regular';

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

select SUM(Marketing) as TotalSpend_Marketing, ProductId from fact group by


ProductId having ProductId=1;
select Sum(Marketing) as TotalSpend_Marketing from fact where ProductId=1;

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


Select MIN(sales) as Min_Sale from fact; --- wrong
select MIN(F.sales) as Min_Sale,P.Product from Product P join Fact F on
P.ProductId=F.ProductId GROUP BY P.Product

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


Select MAX(COGS)as Max_COGS FROM Fact;

---- Display the details of the product where product type is coffee.
Select * from Product where Product_Type='Coffee';

---- Display the details where total expenses are greater than 40.
Select * from fact where Total_Expenses>40 order by Total_Expenses desc;

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

Select AVG(SALES) as Avg_Sales, Area_Code from fact group by Area_Code having


Area_Code=719;
Select AVG(SALES) as Avg_Sales from fact where Area_Code=719;

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


Select SUM(F.Profit) as Total_Profit,L.State from fact F join Location L on
F.Area_Code=L.Area_Code group by L.State having L.State='Colorado';
Select SUM(F.Profit) as Total_Profit from fact F join Location L on
F.Area_Code=L.Area_Code where L.State='Colorado';

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

Select AVG(F.Inventory) as AVG_Inventory,P.ProductId from Product P join Fact F on


P.ProductId=F.ProductId GROUP BY P.ProductId
Select AVG(F.Inventory) as AVG_Inventory,F.ProductId from Product P join Fact F on
P.ProductId=F.ProductId GROUP BY F.ProductId order by f.ProductId;
Select AVG(Inventory) as AVG_Inventory,ProductId from Fact GROUP BY ProductId order
by ProductId;

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


Select *from Location order by State;
Select distinct(State) from Location order by state;
-----Display the average budget of the Product where the average budget margin
should be greater than 100.

Select * from fact;


Select AVG(Budget_Margin) as AVG_Budget_Margin from Fact group by ProductId;
Select AVG(Margin) as AVG_Budget_Product, AVG(Budget_Margin) as
AVG_Budget_Margin,ProductId from Fact group by ProductId having
AVG(Budget_Margin)>100

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

Select SUM(Sales) as Total_Sales from Fact where Date='2010-01-01';

--- Display the average total expense of each product ID on an individual date.
Select AVG(Total_Expenses) as Avg_Total_Expenses,ProductId,date from Fact group by
ProductId,date order by ProductId;

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

Select
F.date,P.ProductId,P.Product_Type,P.Product,F.Sales,F.Profit,L.state,L.area_Code
from Product P join fact F on P.ProductId=F.ProductId join Location L on
F.Area_Code=L.Area_Code;

--- Display the rank without any gap to show the sales wise rank.
Select PRODUCTId,Sales,Profit, DENSE_RANK() over (order by Sales desc) as
Sales_Rank FROM fact;

--- Find the state wise profit and sales.

select * from fact;


select * from location;
Select * from product;

Select Sum(Profit) as Total_Profit,Sum(Sales) as Total_Sales, State from Fact join


Location on Fact.Area_Code=Location.Area_Code group by State;

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

Select Sum(Profit) as Total_Profit,Sum(Sales) as Total_Sales, L.State, P.Product


from Fact F join Location L on F.Area_Code=L.Area_Code join product P on
F.Productid = P.Productid group by L.State,P.Product;

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

You might also like