0% found this document useful (0 votes)
43 views7 pages

Sales and Inventory Analysis Queries

The document contains a series of SQL queries aimed at extracting various sales and product statistics from a database. Key queries include aggregating product counts by category, calculating total sales amounts, and analyzing vendor pricing. Additionally, there are queries for order statistics, employee sales performance, and restocking priorities based on product availability and order history.

Uploaded by

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

Sales and Inventory Analysis Queries

The document contains a series of SQL queries aimed at extracting various sales and product statistics from a database. Key queries include aggregating product counts by category, calculating total sales amounts, and analyzing vendor pricing. Additionally, there are queries for order statistics, employee sales performance, and restocking priorities based on product availability and order history.

Uploaded by

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

select b.

CategoryID,CategoryDescription,sum(QuantityOnHand) as ProductCount from


products a
left outer join categories b
on [Link] = [Link]
group by [Link];
select CategoryDescription,sum(RetailPrice*QuantityOnHand) as TotalAmount from
products a
left outer join categories b
on [Link] = [Link]
group by CategoryDescription;
select vendname,avg(WholesalePrice) as averageprice from vendors a
left outer join product_vendors b
on [Link] = [Link]
group by vendname
order by averageprice desc;
select [Link],sum(QuotedPrice*QuantityOrdered),OrderDate as totalvalue
from order_details a
left outer join orders b
on [Link] = [Link]
group by [Link];
select ordernumber, orderdate , concat(custfirstname,custlastname) as FullName from
orders a
left outer join customers b
on [Link]= [Link]
where month(orderdate) in (1,2,3); /* there was no order placed in 2023*/
select avg(shipdate-orderdate) as AverageValue from orders;
select ordernumber, orderdate, concat(CustFirstName,custlastname) from orders a
left outer join customers b
on [Link] = [Link]
where ShipDate - OrderDate = 0;
SELECT
CONCAT([Link], ' ', [Link]) AS EmployeeFullName,
[Link],
SUM([Link] * [Link]) AS TotalSalesAmount,
SUM([Link]) AS TotalProductsSold,
AVG([Link] * [Link]) AS AvgOrderValue
FROM
Employees e
JOIN
Orders o ON [Link] = [Link]
JOIN
Order_Details od ON [Link] = [Link]
JOIN
Products p ON [Link] = [Link]
JOIN
Categories c ON [Link] = [Link]
WHERE
YEAR([Link]) = 2017
GROUP BY
[Link], [Link], [Link]
HAVING
SUM([Link]) >= 5
AND SUM([Link] * [Link]) > 1000
ORDER BY
EmployeeFullName ASC,
TotalSalesAmount DESC;
/*solved with the help of ai*/
/* i have no idea how to solve question 10 and 11 and the answer ai provides is so over
the top and out of my league that i dont even understand it.
none the less here is the answer ai provided*/
WITH MonthlySales AS (
SELECT
[Link] AS MonthName,
[Link],
COUNT(DISTINCT [Link]) AS OrderCount,
COUNT(DISTINCT [Link]) AS UniqueCustomers,
COALESCE(SUM([Link] * [Link]), 0) AS TotalSales,
CASE
WHEN COUNT(DISTINCT [Link]) = 0 THEN 0
ELSE COALESCE(SUM([Link] * [Link]), 0) /
COUNT(DISTINCT [Link])
END AS AvgOrderValue
FROM
ztblMonths m
LEFT JOIN
Orders o ON [Link] BETWEEN [Link] AND [Link] AND
YEAR([Link]) = 2017
LEFT JOIN
Order_Details od ON [Link] = [Link]
WHERE
[Link] = 2017
GROUP BY
[Link], [Link]
)

SELECT
MonthName,
OrderCount,
UniqueCustomers,
TotalSales,
AvgOrderValue,
CASE
WHEN LAG(TotalSales) OVER (ORDER BY MonthNumber) = 0 THEN NULL
ELSE ROUND((TotalSales - LAG(TotalSales) OVER (ORDER BY
MonthNumber)) /
LAG(TotalSales) OVER (ORDER BY MonthNumber) * 100, 2)
END AS MoM_PercentageChange
FROM
MonthlySales
ORDER BY
MonthNumber;
WITH ProductOrderStats AS (
SELECT
[Link],
COUNT(DISTINCT [Link]) AS OrderCountLast6Months,
SUM([Link]) AS TotalQuantityOrderedLast6Months
FROM
Products p
JOIN
Order_Details od ON [Link] = [Link]
JOIN
Orders o ON [Link] = [Link]
WHERE
[Link] BETWEEN '2017-07-01' AND '2017-12-31'
GROUP BY
[Link]
),
VendorPricing AS (
SELECT
[Link],
[Link],
[Link],
ROW_NUMBER() OVER (PARTITION BY [Link] ORDER BY
[Link]) AS PriceRank
FROM
Product_Vendors pv
JOIN
Vendors v ON [Link] = [Link]
)

SELECT
[Link],
[Link],
[Link],
COALESCE(pos.OrderCountLast6Months, 0) AS OrderCountLast6Months,
COALESCE(pos.TotalQuantityOrderedLast6Months, 0) AS
TotalQuantityOrderedLast6Months,
ROUND(AVG([Link]), 1) AS AvgDaysToDeliver,
MIN(CASE WHEN [Link] = 1 THEN [Link] END) AS
LowestPriceVendor,
MIN(CASE WHEN [Link] = 1 THEN [Link] END) AS
LowestWholesalePrice,
MIN(CASE WHEN [Link] = 2 THEN [Link] END) AS
SecondLowestWholesalePrice,
CASE
WHEN COUNT(DISTINCT [Link]) > 1 THEN
MIN(CASE WHEN [Link] = 2 THEN [Link] END) -
MIN(CASE WHEN [Link] = 1 THEN [Link] END)
ELSE NULL
END AS PriceDifference,
CASE
WHEN [Link] < 10 AND COALESCE(pos.OrderCountLast6Months,
0) > 10 THEN 'High'
WHEN [Link] < 20 AND COALESCE(pos.OrderCountLast6Months,
0) > 5 THEN 'Medium'
ELSE 'Low'
END AS RestockingPriority
FROM
Products p
JOIN
Categories c ON [Link] = [Link]
LEFT JOIN
ProductOrderStats pos ON [Link] = [Link]
LEFT JOIN
Product_Vendors pv ON [Link] = [Link]
LEFT JOIN
VendorPricing vp ON [Link] = [Link]
GROUP BY
[Link], [Link], [Link], [Link],
pos.OrderCountLast6Months, pos.TotalQuantityOrderedLast6Months
ORDER BY
CASE RestockingPriority
WHEN 'High' THEN 1
WHEN 'Medium' THEN 2
ELSE 3
END,
COALESCE(pos.TotalQuantityOrderedLast6Months, 0) DESC;

You might also like