Incorrect query result - SQL Server 2016

Naomi Nosonovsky 8,881 Reputation points
2022-03-30T13:52:28.047+00:00

I'm seeing incorrect result returned by a query I just ran this morning in production (I ran the same query last night couple of times).

Here is the query:

SELECT   DATEDIFF(minute, LAG(ADMIN_AUDIT.admin_audit_dttm)   
OVER (PARTITION BY ADMIN_AUDIT.job_nbr, ADMIN_AUDIT.process_txt, ADMIN_AUDIT.sub_process_txt, sp_id ORDER BY ADMIN_AUDIT.admin_audit_dttm, ADMIN_AUDIT.admin_audit_id), ADMIN_AUDIT.admin_audit_dttm)   
AS [StepTime (min.)], * FROM dbo.ADMIN_AUDIT   
WHERE ADMIN_AUDIT.admin_audit_dttm > '20220329'  
AND ADMIN_AUDIT.sub_process_txt = 'usp_Load_MED_CLAIM_PROF'  
ORDER BY ADMIN_AUDIT.admin_audit_dttm, ADMIN_AUDIT.admin_audit_id, sp_id  

I'm examining the first column to see which step of the procedure takes longest time (I already identified it, now need to figure out how to improve that particular query). Anyway, this is the result:

188370-image.png

The sp_id is different in these two rows (these are two different processes). Why would I get the incorrect result in that query (the other similar stop/start return NULL correctly in the first column)?

188486-image.png

Thanks in advance.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
SQL Server | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Erland Sommarskog 128.7K Reputation points MVP Volunteer Moderator
    2022-03-30T21:47:18.877+00:00

    You are only showing us rows 888 and 889 in the result set. Apparently the same combo of ADMIN_AUDIT.job_nbr, ADMIN_AUDIT.process_txt, ADMIN_AUDIT.sub_process_txt, sp_id ran around 2022-03-29 04:16 as well.


0 additional answers

Sort by: Most helpful

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.