Professional Documents
Culture Documents
Write a query or queries using Transact-SQL syntax to answer the following questions. You may
use as many queries as you feel necessary as long as all the required values are computed. Please
submit your answers to the recruiter in a .txt file.
a) For sales in 2019, output the Sales Model with the highest number of units sold
NOTE: Max(count)----it is not working
select SerialPrefixMapping.sales_Model,count(SALESDATA.Serialnumber)
from SALESDATA inner join SerialPrefixMapping
on SUBSTRING(SALESDATA.serialNumber, 1, 3)=SerialPrefixMapping SerialPrefix
where salesdate like '%2019'
group by sales_Model;
b) For sales in 2017 through 2019, output the top 5 Product Families with the highest
average Sale Price, excluding Product Families that have fewer than 10 units sold overall
SELECT TOP 5 product_Family , AVG(salesprice) as avg_sales_price
FROM SerialPrefixMapping INNER JOIN salesdata
ON SUBSTRING(SALESDATA.serialNumber, 1, 3)=SerialPrefixMapping.serial_Prefix
where SalesDate between '2017-01-01 ' and '2019-12-31'
group by product_family
having avg(salesprice) >10;
c) For each Product Family, produce the average of units sold per day over the last 30 days
from today
select product_Family, salesdate,avg(salesPrice)
from salesdata INNER JOIN SerialPrefixMapping
ON SUBSTRING(SALESDATA.serialNumber, 1, 3)=SerialPrefixMapping.serial_Prefix
WHERE CAST(salesdate AS DATE) >= GETDATE() -30
GROUP BY PRODUCT_FAMILY,salesdate ;
d) For each product family, generate the ratio of last 12 months sales divided by previous 12
months sales