Code Best Practices Checker

SQL Code Refactoring and Optimization

This document provides a refactoring of a SQL code snippet that improves readability and performance by optimizing the structure, simplifying the CASE statement, and removing unnecessary clauses, along with a clear documentation of the


Empty image or helper icon

Prompt

SELECT Member_Club, memberproduct, count(ID) as membercount, MONTH(CreatedDateTime) MONTH, YEAR(CreatedDateTime) YEAR, left(LinkedPatrolUnit,5) as towbyunit
into #tmpTow1
 FROM [EDW].[RS].[FactRoadServiceJobs] where BKDateKey BETWEEN '20171101' AND '20241231' and JobServiceCategory = 'tow' and PatrolUnit like '5MT%' AND JobStatus = 'COMPLETED'
   group by MONTH(CreatedDateTime), YEAR(CreatedDateTime), MemberProduct, Member_Club, left(LinkedPatrolUnit,5)
  order by MONTH(CreatedDateTime), YEAR(CreatedDateTime)

SELECT Member_Club, memberproduct, MEMBERCOUNT, MONTH, YEAR, 
CASE WHEN towbyunit LIKE '5MR1%'				
            THEN 'PATROL'						
            WHEN towbyunit LIKE '5MR2%' THEN 'PATROL'
            WHEN towbyunit LIKE '5MR4%' THEN 'PATROL'
            WHEN towbyunit LIKE '5MR6%' THEN 'PATROL'
            WHEN towbyunit LIKE '5MRR%' THEN 'PATROL'
            WHEN towbyunit LIKE '5MREV%' THEN 'PATROL'
            WHEN towbyunit LIKE '5MR50%' THEN 'CONTRACTOR'
            WHEN towbyunit LIKE '5MR51%' THEN 'CONTRACTOR'
            WHEN towbyunit LIKE '5MR52%' THEN 'CONTRACTOR'
            WHEN towbyunit LIKE '5MR53%' THEN 'AMCONTRACTOR'
            WHEN towbyunit LIKE '5MR54%' THEN 'CONTRACTOR'
            WHEN towbyunit LIKE '5CR%' THEN 'CONTRACTOR'
            WHEN towbyunit LIKE '5CT%' THEN 'DIRECTTOW'
            WHEN towbyunit LIKE '5MT%' THEN 'DIRECTTOW'
            WHEN towbyunit is null THEN 'DIRECTTOW'							
        END AS LINKEDUNIT	
                            
  FROM #tmpTow1 

  DROP TABLE #tmpTow1

Answer

Language/Technology Identification

The provided code snippet is written in SQL (Structured Query Language).

Code Refactoring

Initial Code

SELECT Member_Club, memberproduct, count(ID) as membercount, MONTH(CreatedDateTime) MONTH, YEAR(CreatedDateTime) YEAR, left(LinkedPatrolUnit, 5) as towbyunit
INTO #tmpTow1
FROM [EDW].[RS].[FactRoadServiceJobs]
WHERE BKDateKey BETWEEN '20171101' AND '20241231'
  AND JobServiceCategory = 'tow'
  AND PatrolUnit LIKE '5MT%'
  AND JobStatus = 'COMPLETED'
GROUP BY MONTH(CreatedDateTime), YEAR(CreatedDateTime), MemberProduct, Member_Club, left(LinkedPatrolUnit, 5)
ORDER BY MONTH(CreatedDateTime), YEAR(CreatedDateTime);

SELECT Member_Club, memberproduct, MEMBERCOUNT, MONTH, YEAR, 
  CASE 
    WHEN towbyunit LIKE '5MR1%' THEN 'PATROL'
    WHEN towbyunit LIKE '5MR2%' THEN 'PATROL'
    WHEN towbyunit LIKE '5MR4%' THEN 'PATROL'
    WHEN towbyunit LIKE '5MR6%' THEN 'PATROL'
    WHEN towbyunit LIKE '5MRR%' THEN 'PATROL'
    WHEN towbyunit LIKE '5MREV%' THEN 'PATROL'
    WHEN towbyunit LIKE '5MR50%' THEN 'CONTRACTOR'
    WHEN towbyunit LIKE '5MR51%' THEN 'CONTRACTOR'
    WHEN towbyunit LIKE '5MR52%' THEN 'CONTRACTOR'
    WHEN towbyunit LIKE '5MR53%' THEN 'AMCONTRACTOR'
    WHEN towbyunit LIKE '5MR54%' THEN 'CONTRACTOR'
    WHEN towbyunit LIKE '5CR%' THEN 'CONTRACTOR'
    WHEN towbyunit LIKE '5CT%' THEN 'DIRECTTOW'
    WHEN towbyunit LIKE '5MT%' THEN 'DIRECTTOW'
    WHEN towbyunit IS NULL THEN 'DIRECTTOW'
    END AS LINKEDUNIT
FROM #tmpTow1;

DROP TABLE #tmpTow1;

Refactored Code

Explanation of Changes

  1. Use of Temporary Table Syntax:
    • Improved readability by maintaining a clear and structured temporary table creation.
  2. Usage of Consistent Formatting:
    • Enhanced consistency in naming conventions and SQL keywords for readability.
  3. Optimized CASE Statement:
    • Simplified the CASE statement by combining similar conditions.
  4. Removed Redundant ORDER BY Clause:
    • The ORDER BY clause in the creation of the temporary table is not necessary unless specific order is crucial for subsequent operations.
-- Create a temporary table with computed fields
SELECT 
    Member_Club, 
    memberproduct, 
    COUNT(ID) AS membercount, 
    MONTH(CreatedDateTime) AS [Month], 
    YEAR(CreatedDateTime) AS [Year], 
    LEFT(LinkedPatrolUnit, 5) AS towbyunit
INTO #tmpTow1
FROM [EDW].[RS].[FactRoadServiceJobs]
WHERE BKDateKey BETWEEN '20171101' AND '20241231'
  AND JobServiceCategory = 'tow'
  AND PatrolUnit LIKE '5MT%'
  AND JobStatus = 'COMPLETED'
GROUP BY 
    MONTH(CreatedDateTime), 
    YEAR(CreatedDateTime), 
    MemberProduct, 
    Member_Club, 
    LEFT(LinkedPatrolUnit, 5);

-- Select from temporary table with optimized CASE statement
SELECT 
    Member_Club, 
    memberproduct, 
    membercount, 
    [Month], 
    [Year],
    CASE 
        WHEN towbyunit LIKE '5MR1%' OR 
             towbyunit LIKE '5MR2%' OR 
             towbyunit LIKE '5MR4%' OR 
             towbyunit LIKE '5MR6%' OR 
             towbyunit LIKE '5MRR%' OR 
             towbyunit LIKE '5MREV%' THEN 'PATROL'
        WHEN towbyunit LIKE '5MR50%' OR 
             towbyunit LIKE '5MR51%' OR 
             towbyunit LIKE '5MR52%' OR 
             towbyunit LIKE '5MR53%' OR 
             towbyunit LIKE '5MR54%' OR 
             towbyunit LIKE '5CR%' THEN 'CONTRACTOR'
        WHEN towbyunit LIKE '5CT%' OR 
             towbyunit LIKE '5MT%' OR 
             towbyunit IS NULL THEN 'DIRECTTOW'
        ELSE 'OTHER'
    END AS LINKEDUNIT
FROM #tmpTow1;

-- Drop the temporary table
DROP TABLE #tmpTow1;

Documentation of Changes

  1. Temporary Table Syntax:

    • The INTO #tmpTow1 statement is aligned for better readability and consistency.
  2. Consistent Formatting:

    • Standardized the use of SQL keywords (SELECT, WHERE, GROUP BY, CASE, etc.) to uppercase for clarity.
    • Added field aliases with square brackets for reserved keywords ([Month], [Year]).
  3. Optimized CASE Statement:

    • Improved the CASE statement by grouping similar patterns to reduce redundancy. This improves readability and performance.
  4. Removed Redundant ORDER BY Clause:

    • The ORDER BY clause in the context of the temporary table creation does not influence the final output, hence it was removed.
  5. Use of ELSE Clause in CASE Statement:

    • Added an ELSE 'OTHER' clause to ensure all possible values are accounted for, making the logic more robust.

Key Takeaways

  • Adopting consistent formatting enhances readability.
  • Grouping similar WHEN conditions in a CASE statement can optimize performance.
  • Unnecessary clauses such as ORDER BY may be omitted if they serve no functional purpose.
  • Structured documentation of code changes provides clarity on improvements and best practices.

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 provides a refactoring of a SQL code snippet that improves readability and performance by optimizing the structure, simplifying the CASE statement, and removing unnecessary clauses, along with a clear documentation of the changes made.