You are on page 1of 2

Instructions to Candidate:

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.

Table Name: SalesData


An entry is made into this table when a machine is sold

Field Name Type Example Data


Sales.Date varchar(255) “2007-01-01”
Serial.Number varchar(255) “QPF01219”
Sale.Price float 12345.67

Table Name: SerialPrefixMapping


The first 3 characters of a SerialNumber define the product hierarchy.
This table maps the 3 characters to the overall product hierarchy.

Field Name Type Example Data


Serial.Prefix varchar(255) “FGD”
Product.Family varchar(255) “MWL”
Sales.Model varchar(255) “972-14”
Product.Group varchar(255) “GCI”

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;

Could you correct this query please max(count(serialNumber))

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

select product_Family,year(salesdate) as YEAR, Month(salesdate) as Month,


SUM(salesPrice) ,
LAG(SUM(salesPrice),12) OVER (ORDER BY YEAR(salesdate), Month(salesdate))
from salesdata INNER JOIN SerialPrefixMapping
ON SUBSTRING(SALESDATA.serialNumber, 1, 3)=SerialPrefixMapping.serial_Prefix
where YEAR(salesdate) IN (2017,2018)
GROUP BY PRODUCT_FAMILY;

You might also like