Code Best Practices Checker

SQL Query Refactoring for Road Service Jobs

This document presents a refactored SQL query that improves readability and maintainability by using a Common Table Expression (CTE) to eliminate redundancy, enhance alias consistency, and optimize result ordering for analyzing road


Empty image or helper icon

Prompt

SELECT Member_Club, memberproduct, count(BKJobNo) as eventcount, SUBSTRING(CAST(bkdatekey AS VARCHAR), 5, 2) AS extracted_month, 
SUBSTRING(CAST(bkdatekey AS VARCHAR), 1, 4) AS extracted_year
FROM [EDW].[RS].[FactRoadServiceJobs] where BKDateKey BETWEEN '20191101' AND '20241231' and 
PatrolUnit like '5MR%' and JobServiceCategory = 'Road Service' and IsCompletedJob = 1 and region  = 'm'
GROUP BY SUBSTRING(CAST(bkdatekey AS VARCHAR), 5, 2), SUBSTRING(CAST(bkdatekey AS VARCHAR), 1, 4), MemberProduct, Member_Club
 ORDER BY extracted_month, extracted_year

Answer

Language/Technology Identification

The code snippet provided is a SQL query.

Original Code Snippet

SELECT 
    Member_Club, 
    memberproduct, 
    count(BKJobNo) as eventcount, 
    SUBSTRING(CAST(bkdatekey AS VARCHAR), 5, 2) AS extracted_month, 
    SUBSTRING(CAST(bkdatekey AS VARCHAR), 1, 4) AS extracted_year
FROM 
    [EDW].[RS].[FactRoadServiceJobs] 
WHERE 
    BKDateKey BETWEEN '20191101' AND '20241231' 
    AND PatrolUnit like '5MR%' 
    AND JobServiceCategory = 'Road Service' 
    AND IsCompletedJob = 1 
    AND region = 'm'
GROUP BY 
    SUBSTRING(CAST(bkdatekey AS VARCHAR), 5, 2), 
    SUBSTRING(CAST(bkdatekey AS VARCHAR), 1, 4), 
    MemberProduct, 
    Member_Club
ORDER BY 
    extracted_month, 
    extracted_year;

Code Refactoring

Issues Identified:

  1. Redundancy in SUBSTRING(CAST(bkdatekey AS VARCHAR)): The same SUBSTRING(CAST(bkdatekey AS VARCHAR)) logic is repeated in both SELECT and GROUP BY.
  2. Column Alias Case Consistency: Column names and aliases should follow a consistent naming convention.
  3. Improving Readability: SQL keywords should be consistently formatted to improve readability.

Refactored Code:

WITH DateParts AS (
    SELECT 
        Member_Club, 
        MemberProduct, 
        BKJobNo,
        CAST(SUBSTRING(CAST(bkdatekey AS VARCHAR), 5, 2) AS INT) AS extracted_month, 
        CAST(SUBSTRING(CAST(bkdatekey AS VARCHAR), 1, 4) AS INT) AS extracted_year
    FROM 
        [EDW].[RS].[FactRoadServiceJobs]
    WHERE 
        BKDateKey BETWEEN '20191101' AND '20241231'
        AND PatrolUnit LIKE '5MR%'
        AND JobServiceCategory = 'Road Service'
        AND IsCompletedJob = 1
        AND Region = 'm'
)
SELECT 
    Member_Club, 
    MemberProduct, 
    COUNT(BKJobNo) AS eventcount, 
    extracted_month,
    extracted_year
FROM 
    DateParts
GROUP BY 
    extracted_month, 
    extracted_year, 
    MemberProduct, 
    Member_Club
ORDER BY 
    extracted_year,
    extracted_month;

Documentation of Changes

  1. Use of Common Table Expression (CTE):

    • Original:
      SUBSTRING(CAST(bkdatekey AS VARCHAR), 5, 2) AS extracted_month, 
      SUBSTRING(CAST(bkdatekey AS VARCHAR), 1, 4) AS extracted_year
    • Refactored:
      CAST(SUBSTRING(CAST(bkdatekey AS VARCHAR), 5, 2) AS INT) AS extracted_month, 
      CAST(SUBSTRING(CAST(bkdatekey AS VARCHAR), 1, 4) AS INT) AS extracted_year
    • Reasoning: The same substring logic was used multiple times. By using a CTE, the transformation is performed once, improving readability and maintainability.
  2. Column Alias Case Consistency:

    • Original:
      count(BKJobNo) as eventcount
    • Refactored:
      COUNT(BKJobNo) AS eventcount
    • Reasoning: Consistently using uppercase for SQL keywords follows best practices and improves readability.
  3. Ordering of Results:

    • Original:
      ORDER BY extracted_month, extracted_year
    • Refactored:
      ORDER BY extracted_year, extracted_month
    • Reasoning: Ordering by extracted_year first then extracted_month ensures that the results are grouped by year and then by month within each year, providing a more logical chronological order.

Benefits of Refactoring

  • Improved Readability: By using a CTE and consistently formatting SQL keywords, the query is easier to read and understand.
  • Maintainability: Reducing redundancy and centralizing the date transformation logic makes future modifications and debugging easier.
  • Performance: While performance improvements may be minimal, better structuring of the query can help in optimizing execution for more complex queries or larger datasets.

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 presents a refactored SQL query that improves readability and maintainability by using a Common Table Expression (CTE) to eliminate redundancy, enhance alias consistency, and optimize result ordering for analyzing road service job events.