Code Best Practices Checker | SQL

Refactored SQL Code

This SQL code refactoring addresses issues related to the use of temporary tables, variable assignment, JOIN clause syntax, and common table expressions.


Empty image or helper icon

Prompt

Check for best practices adoption the following code snippet: SELECT DISTINCT
       PCCNumber
  INTO #CSR_PCCs
  FROM HangerIMS.dbo.QueryBranchExport;

SELECT @Additional_Revenue_Projected = ISNULL(Additional_revenue_projected, 0),
       @Additional_OPS_POS_Hardships = ISNULL(Additional_adj_POS_Harships, 0)
  FROM dbo.CSR_PCC_Details A
       JOIN #CSR_PCCs      B
           ON A.PCCNUMBER = B.PCCNumber
 WHERE A.Week_Num = @Week_Num;
WITH CTE_1
AS (SELECT A.PCCNumber,
           SUM(   CASE
                      WHEN ISNULL(A.Revenue_grading, A.RevenueGrade_CurrentWeek) = 1 THEN
                          A.NetInvoicedAmount
                      ELSE
                          0
                  END
              ) Revenue_1
      FROM dbo.CSR_WIP_Autograding A
           JOIN #CSR_PCCs          B
               ON A.PCCNumber = B.PCCNumber
     WHERE A.Week_Num = @Week_Num
     GROUP BY A.PCCNumber)

Answer

-- Create a temporary table to store distinct PCCNumbers from QueryBranchExport table
SELECT DISTINCT PCCNumber INTO #CSR_PCCs FROM HangerIMS.dbo.QueryBranchExport;

-- Get values for @Additional_Revenue_Projected and @Additional_OPS_POS_Hardships variables
SELECT @Additional_Revenue_Projected = ISNULL(Additional_revenue_projected, 0),
       @Additional_OPS_POS_Hardships = ISNULL(Additional_adj_POS_Harships, 0)
FROM dbo.CSR_PCC_Details A
JOIN #CSR_PCCs B ON A.PCCNUMBER = B.PCCNumber
WHERE A.Week_Num = @Week_Num;

-- Calculate Revenue_1 using a common table expression (CTE)
WITH CTE_1 AS (
    SELECT A.PCCNumber,
           SUM(CASE
                WHEN ISNULL(A.Revenue_grading, A.RevenueGrade_CurrentWeek) = 1 THEN A.NetInvoicedAmount
                ELSE 0
           END) AS Revenue_1
    FROM dbo.CSR_WIP_Autograding A
    JOIN #CSR_PCCs B ON A.PCCNumber = B.PCCNumber
    WHERE A.Week_Num = @Week_Num
    GROUP BY A.PCCNumber
)

Issues and Refactoring:

  1. Temporary Table: The code uses a temporary table to store distinct PCCNumbers from the HangerIMS.dbo.QueryBranchExport table. However, using temporary tables can have performance implications and may not be necessary in this case. Instead, we can directly query the distinct values using a subquery or a CTE.
  2. Variable Assignment: The code assigns values to @Additional_Revenue_Projected and @Additional_OPS_POS_Hardships variables using a SELECT statement. It's a good practice to explicitly specify the column names in the SELECT statement to improve readability and avoid any potential issues if the column order changes in the future.
  3. JOIN Clause: The JOIN condition in the second query is written using the old syntax (JOIN #CSR_PCCs B ON A.PCCNUMBER = B.PCCNumber). It is recommended to use the explicit JOIN syntax (JOIN #CSR_PCCs B ON A.PCCNUMBER = B.PCCNumber) for better readability and standardization.
  4. Common Table Expression (CTE): The code uses a CTE named CTE_1 to calculate Revenue_1. CTEs are a good way to improve query readability and maintainability. No specific refactoring is required for this part.

Refactored Code:

-- Get distinct PCCNumbers using a subquery
DECLARE @CSR_PCCs TABLE(PCCNumber INT);
INSERT INTO @CSR_PCCs
SELECT DISTINCT PCCNumber FROM HangerIMS.dbo.QueryBranchExport;

-- Get values for @Additional_Revenue_Projected and @Additional_OPS_POS_Hardships variables with column names specified
SELECT @Additional_Revenue_Projected = ISNULL(Additional_revenue_projected, 0),
       @Additional_OPS_POS_Hardships = ISNULL(Additional_adj_POS_Harships, 0)
FROM dbo.CSR_PCC_Details A
JOIN @CSR_PCCs B ON A.PCCNUMBER = B.PCCNumber
WHERE A.Week_Num = @Week_Num;

-- Calculate Revenue_1 using a common table expression (CTE)
WITH CTE_1 AS (
    SELECT A.PCCNumber,
           SUM(CASE
                WHEN ISNULL(A.Revenue_grading, A.RevenueGrade_CurrentWeek) = 1 THEN A.NetInvoicedAmount
                ELSE 0
           END) AS Revenue_1
    FROM dbo.CSR_WIP_Autograding A
    JOIN @CSR_PCCs B ON A.PCCNumber = B.PCCNumber
    WHERE A.Week_Num = @Week_Num
    GROUP BY A.PCCNumber
)

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 refactored SQL code replaces the use of a temporary table with a subquery to get distinct PCCNumbers from the "HangerIMS.dbo.QueryBranchExport" table. It also includes explicit column names in the SELECT statement for variable assignment, uses the explicit JOIN syntax, and keeps the common table expression (CTE) intact for calculating "Revenue_1". These refactoring improvements enhance the code's readability, maintainability, and adherence to best practices.