FILTER Quarter 1 and 2 Transactions in SQL Server

Welcome to Excel Jet Consult blog post. In this blog, we are going to learn how to filter the quarter 1 and 2 transactions in SQL Server 2022. Let’s get started

SQL Server Table

The screenshot below all the records in the factSalesTable of the SalesData database. The table contains 55,552 records as seen at the bottom right of the screenshot

Filter Quarter 1 and 2 Transactions

To filter the quarter 1 and 2 transactions from the above table, execute this query:

  • SELECT * FROM factSalesTable WHERE (DATEPART(MONTH,DATE) % 12 + 2)/3 IN (1,2);
  • Select the query and Execute

Explanation:

  • The SELECT¬† instruct the database to fetch all columns from the factSalesTable for rows meeting specific conditions.
  • The FROM factSalesTable specifies the source table, named factSalesTable.
  • The WHERE is used to filter rows that meet conditions specified below
  • The (DATEPART(MONTH, DATE) % 12 + 2)/3 IN (1,2) condition extracts the month from the “DATE” column, converts it to values from 2 to 13, divides by 3 which will return Quarters 1,2,3 and 4, and then the IN operator is used checks if the result is 1 or 2 which implies if it is Quarter 1 or 2.

From the screenshot below, we have successfully filtered the table for quarter 1 and 2 transactions which returned 27,570 records

See you in the next blog post.

 

 

Posted in Blog.

Leave a Reply

Your email address will not be published. Required fields are marked *