Unexplainable results

Naomi Nosonovsky 8,771 Reputation points
2025-10-19T19:10:55.1333333+00:00

Good day,

I have a complex stored procedure. This procedure returns different results when I add extra condition for specific NPI vs returning without this extra condition.

Automation OHCS Professional Rendering Yes OHCS Partial Term Termed 2025-03-21 2020-08-20 NULL 2025-11-05 2025-11-05 2025-11-05 850879488 1114762259 240341 ASPINALL IDA F ZEEL MASSAGE THERAPY AS 61 225700000X MT014765 GA 2023-10-30 2920 EDGEWOOD CIR STE 2 COLUMBUS GA 31907 PO BOX 8482 PASADENA CA 91109 N MT MT N T 2025-11-05 00:00:00.000 2025-03-21 00:00:00.000 MUSCOGEE 240341610711 610711 2025-03-21 00:00:00.000 2025-04-10 00:00:00.000 2025-03-21 00:00:00.000 NULL 8774389335 6466194461 F 2025-11-04 00:00:00.000 2025-03-21 00:00:00.000 NULL N

Automation OHCS Professional Solo Yes OHCS Term Termed 2025-04-10 2025-04-10 NULL 2025-11-05 2025-11-05 NULL 309048968 1114762259 240341 ASPINALL IDA F MASSAGE DIRECT AS 61 225700000X MT014765 GA 2023-10-30 2920 EDGEWOOD CIR STE 2 COLUMBUS GA 31907 2920 EDGEWOOD CIR STE 2 COLUMBUS GA 31907 N MT MT N T 2025-11-05 00:00:00.000 2025-04-10 00:00:00.000 MUSCOGEE 240341617632 617632 2025-04-10 00:00:00.000 NULL 2025-04-10 00:00:00.000 NULL 4706357655 F 2025-11-04 00:00:00.000 2025-03-21 00:00:00.000 NULL N

I highlighted values of interest.

However, when I select without this extra condition, the license data are not selected and the specialty shows DC. How can we explain such a difference ?

SQL Server | SQL Server Transact-SQL
{count} votes

Answer accepted by question author
  1. Erland Sommarskog 127.4K Reputation points MVP Volunteer Moderator
    2025-10-19T21:05:29.4233333+00:00

    Basically, there are two problems. The licenses columns show populated with the filter but otherwise have no results in the whole set

    Well,

    CASE WHEN cte.license_number ='' THEN '' ELSE MIN(cte.license_number) OVER () END
    

    If any row has a blank license number, that is the minimum value for that column. If you add a filter, you may be filter out those blank values.

    For specialty we need to get the first value alphabetically which is what my MIN code is supposed to achieve. The query with the NPI filter correctly returns 'MT', but the whole result set returns 'DC' for all records.

    Isn't DC before MT alphabetically? But if the filter filters out DC, MT might come first.

    Keep in mind that the filter is applied before all those MIN OVER() in the SELECT list.

    If you want more help, please post a script with CREATE TABLE + INSERT statements - and the expected results. And a lot more simplified than the query above.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Azar 30,735 Reputation points MVP Volunteer Moderator
    2025-10-19T19:27:35.0133333+00:00

    Hi there Naomi Nosonovsky

    Thanks for using QandA platform

    When you apply a filter for a specific NPI, the query might narrow down the join results or affect which rows are being matched (for example, an INNER JOIN could eliminate rows without matches). But when you run it without that condition, you might be getting data from a different join path or even from default/null records that lead to the “DC” specialty showing up instead of the correct license details.

    I’d suggest checking your JOIN clauses first — look for INNER vs LEFT joins and confirm that the data relationship between provider, specialty, and license tables is consistent. Also, try running the same query with SET STATISTICS IO ON and SET STATISTICS PROFILE ON to see how SQL Server changes its execution plan with and without the extra condition. Sometimes, even subtle data issues (like duplicate NPIs or mismatched keys) can cause completely different results.

    If this helps kindly accept the answer thanks much

    1 person found this answer helpful.

  2. Naomi Nosonovsky 8,771 Reputation points
    2025-10-19T22:39:10.2266667+00:00

    The above is the insert statement for #FIrstResult table. I posted the query in another comment. I only selected 3 rows out of 200000.

    0 comments No comments

Your answer

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