Welcome to Excel Jet Consult blog. In this blog post, we are going to explore how to write SQL to Analyze 2023 Quarterly Sales Trends by Product in SQL Server. Let’s get started
Below is the table we are going to work with. We have use SELECT * FROM factSalesTable to return all the records.
Analyzing 2023 Quarterly Sales Trends by Product in SQL Server
To analyze 2023 Quarterly Sales Trends by Product, we are going to write the following SQL query and execute:
SELECT Year, DATEPART(QUARTER, Date) AS Quarter, Products, SUM(Total) AS TotalSales FROM factSalesTable WHERE Year = 2018 GROUP BY Year, DATEPART(QUARTER, Date), Products ORDER BY DATEPART(QUARTER, DATE)
SELECT Year, DATEPART(QUARTER, Date) AS Quarter, Products, SUM(Total) AS TotalSales FROM factSalesTable
In the SELECT statement, we specify that we want to display four columns: “Year,” “Quarter” (calculated using DATEPART), “Products,” and the total sales for each combination of year, quarter, and product (aliased as “TotalSales”).
We specify the source table as “factSalesTable.”
The WHERE Year = 2018 clause filters the data to include only transactions from the year 2018. The GROUP BY Year, DATEPART(QUARTER, Date), Products groups the results by three columns: “Year,” the quarter calculated from the “Date” column, and “Products.” This groups sales data by these three dimensions, allowing us to analyze sales trends in detail.
The ORDER BY DATEPART(QUARTER, DATE) sorts the result by the extracted quarter in ascending order.
From the screenshot below, we’ve got the Sales Trend for every quarter in 2018 by Product
From the business point of view, the above query helps to evaluate how each product performed in each quarter of 2018 useful in identifying seasonal trends.