SQL 2022 -- cte + insert doesn't work

Naomi Nosonovsky 8,351 Reputation points
2025-04-11T16:46:56.2133333+00:00

Good day,

I have the following simple SQL statement:

		DELETE FROM stg.stg_facets_solo;

		;WITH cte AS
			(
			SELECT
				*
				, COUNT(*) OVER (PARTITION BY f.taxid) AS cntNpi
				, COUNT(*) OVER (PARTITION BY
									 f.npi
									 , f.taxid
									 , f.facilitygroupname
									 , f.providerlastname
									 , f.providerfirstname
									 , f.providermi
								) AS cntUnique
			FROM
				stg.facets_full f
			)
		INSERT INTO
			stg.stg_facets_solo (load_date, source, hash_diff_status, npi_hk, npi, status)
		SELECT DISTINCT
			   GETDATE()
			   , 'FACETS'
			   , CONVERT(CHAR(32), HASHBYTES('SHA2_256', 'A'), 2)
			   , CONVERT(CHAR(32), HASHBYTES('SHA2_256', npi), 2)
			   , npi
			   , 'A'
		FROM
			   cte 
		WHERE
			   COALESCE(providerlastname, '') <> ''
			   --AND COALESCE(a.facilitygroupname, '') = ''
			   AND UPPER(primarycontractedrelationship) = 'CLINICIAN'
			   AND cte.cntNpi = cte.cntUnique
			   AND NOT EXISTS
		(
			SELECT
				1
			FROM
				stg.facets_full b
			WHERE
				b.taxid = cte.taxid
				AND (b.providerlastname = '' OR cte.providerlastname <> b.providerlastname)
		)
			   ;

Hovewer, when I examine the result, the cteNPI = cteUnique condition is not being observed. What am I doing wrong in my simple statement?

SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
177 questions
{count} votes

Accepted answer
  1. Prasanna Jung Malla 75 Reputation points
    2025-04-11T18:16:10.16+00:00

    Condition not met does not necessarily mean you are doing something wrong!

    If you examine the result of the query you have within your CTE, do you see any records in your result where those two columns have equal value? If you do not see such records but are expecting some records to have equal values between those columns then you may need to revise your CTE.

    Regardless, you can further improve your query as following:

    1. Listing only the column names you intend to use to insert and filter such as npi, providerlastname, and taxid on your CTE instead of *.
    2. Add the filter condition on the CTE to omit any records where providerlastname is empty. You are doing this on your subqueries to determine value you intend to insert into stg.stg_facets_solo table.
    3. You are also inserting the data only for Clinician primary contracted relationship. So this could also be added to the where condition on the CTE.

    Hope that helps!


1 additional answer

Sort by: Most helpful
  1. Naveen Kumar M 175 Reputation points
    2025-04-11T19:11:16.8733333+00:00

    Looking at your SQL statement, I can see why the cte.cntNpi = cte.cntUnique condition might not be working as expected. Here's the issue:

    In your CTE, you're calculating two counts:

    1. cntNpi - counts records per taxid
    2. cntUnique - counts records per combination of npi, taxid, and provider details

    The condition cte.cntNpi = cte.cntUnique is meant to ensure that for a given taxid, all records with that taxid have the same npi and provider details. However, your WHERE clause has additional conditions that might be interfering.

    The main problem is that your EXISTS subquery at the end is checking for other records with the same taxid but different provider names, but this is separate from your count comparison. Key changes:

    1. Moved the COALESCE and primarycontractedrelationship conditions into the initial CTE to reduce the dataset early
    2. Created a second CTE to apply the count comparison and EXISTS condition
    3. Then selected from this filtered CTE for the final insert

    This makes the logic clearer and ensures the count comparison is properly applied before other conditions.

    DELETE FROM stg.stg_facets_solo;
    WITH cte AS (
        SELECT
            *,
            COUNT(*) OVER (PARTITION BY taxid) AS cntNpi,
            COUNT(*) OVER (PARTITION BY npi, taxid, facilitygroupname, providerlastname, providerfirstname, providermi) AS cntUnique
        FROM stg.facets_full
        WHERE COALESCE(providerlastname, '') <> ''
          AND UPPER(primarycontractedrelationship) = 'CLINICIAN'
    ),
    filtered_cte AS (
        SELECT *
        FROM cte
        WHERE cntNpi = cntUnique
          AND NOT EXISTS (
              SELECT 1
              FROM stg.facets_full b
              WHERE b.taxid = cte.taxid
                AND (b.providerlastname = '' OR cte.providerlastname <> b.providerlastname)
          )
    )
    INSERT INTO stg.stg_facets_solo (load_date, source, hash_diff_status, npi_hk, npi, status)
    SELECT DISTINCT
        GETDATE(),
        'FACETS',
        CONVERT(CHAR(32), HASHBYTES('SHA2_256', 'A'), 2),
        CONVERT(CHAR(32), HASHBYTES('SHA2_256', npi), 2),
        npi,
        'A'
    FROM filtered_cte;
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.