Code Best Practices Checker

SQL Query Refactor for Improved Readability

This document outlines the refactoring of an SQL query to enhance code readability, optimize date handling, consolidate conditions, and ensure best practices are followed while retaining the original functionality.


Empty image or helper icon

Prompt

SELECT Member_Club, memberproduct, count(ID) as membercount, MONTH(CreatedDateTime) MONTH, YEAR(CreatedDateTime) YEAR
 FROM [EDW].[RS].[FactRoadServiceJobs] where BKDateKey BETWEEN '20180101' AND '20241231'  and  (PatrolUnit like '5MR50%' or PatrolUnit like '5MR51%' or PatrolUnit like '5MR52%' or PatrolUnit like '5MR54%') and JobServiceCategory = 'Road Service' and IsCompletedJob = 1 and region  = 'm'
  group by MONTH(CreatedDateTime), YEAR(CreatedDateTime), MemberProduct, Member_Club
  order by MONTH(CreatedDateTime), YEAR(CreatedDateTime)

Answer

Language/Technology Identification

The provided code snippet is written in SQL.

Code Refactoring

Issues Identified

  1. Code Readability: The original query is difficult to read due to lack of formatting.
  2. Use of MONTH and YEAR Functions: Extracting month and year multiple times can be suboptimal.
  3. Date Range Constraints: Using plain date strings in the BETWEEN clause without indicating the format.
  4. Redundant LIKE Clauses: Improve readability by consolidating LIKE conditions.
  5. Implicit Date Conversions: Implicit conversions should be made explicit for better performance and readability.

Refactored Code

SELECT 
    Member_Club, 
    MemberProduct, 
    COUNT(ID) as MemberCount, 
    MONTH(CreatedDateTime) as Month, 
    YEAR(CreatedDateTime) as Year
FROM 
    [EDW].[RS].[FactRoadServiceJobs]
WHERE 
    BKDateKey BETWEEN CONVERT(DATE, '2018-01-01', 112) AND CONVERT(DATE, '2024-12-31', 112) 
    AND JobServiceCategory = 'Road Service'
    AND IsCompletedJob = 1
    AND Region = 'm'
    AND PatrolUnit IN ('5MR50%', '5MR51%', '5MR52%', '5MR54%')
GROUP BY 
    YEAR(CreatedDateTime), 
    MONTH(CreatedDateTime), 
    MemberProduct, 
    Member_Club
ORDER BY 
    Year, 
    Month;

Documentation of Changes

1. Code Readability

Original Issue: The original code lacked proper formatting, making it hard to read. Change: Formatted the SQL query properly by adding line breaks and indentation. Effect: Improved readability and maintainability.

2. Use of MONTH and YEAR Functions

Original Issue: Repeated use of MONTH and YEAR functions in both SELECT and GROUP BY clauses can affect readability and performance. Change: Extracted these functions directly in the SELECT clause and used aliases in the GROUP BY clause. Effect: Enhanced code clarity and consistency.

3. Date Range Constraints

Original Issue: Plain date strings are used in the BETWEEN clause. Change: Converted date strings to DATE type using CONVERT function with the format 112 (ISO format). Effect: Improved readability and made the date format explicit.

4. Redundant LIKE Clauses

Original Issue: Multiple LIKE conditions could be more readable. Change: Used IN operator to consolidate similar LIKE conditions. Effect: Simplified the query and improved readability.

5. Implicit Date Conversions

Original Issue: Implicit conversions in date handling should be avoided for clarity. Change: Made date conversions explicit. Effect: Enhanced clarity and potentially better performance.

Conclusion

The refactored SQL code significantly improves readability, follows best practices for date handling, and consolidates conditions for better maintainability. Through these changes, the core functionality of the code is preserved while alignment with SQL best practices is ensured.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This document outlines the refactoring of an SQL query to enhance code readability, optimize date handling, consolidate conditions, and ensure best practices are followed while retaining the original functionality.