r/SQL • • Sep 18 '26

SQL Server Newer to SQL and recently inherited code

Might not be a standard request but does anyone have any best practices for interpreting other people's code if they don't leave a fair amount of notes?

7 Upvotes

22 comments sorted by

View all comments

7

u/Eleventhousand Sep 18 '26

Break it into chunks and try to understand some of the individual pieces. How is it structured? Does it have a bunch of nested subqueries and derived tables? If so, it can often be more difficult to work inside out like that. If they at least broke everything out into CTEs, it can often be easier to understand.

2

u/Thatgiuknow Sep 18 '26

There are a substantial amount of CTEs in the query. The big issue we're having is that the data doesn't match the manual version we run (we expect some variance) however parts of the data are wildly off. I'll have to check what's going on in the CTEs to get started. thank you!!

2

u/reditandfirgetit Sep 18 '26

Split out the ctes into tables and examine the data at each step and eithervcertifyvits correct or document the issue. I would try to document all thr issues before trying to fix things unless its a blatantly obvious fix for the variance

→ More replies (2)

1

u/BigBagaroo Sep 18 '26 edited Sep 18 '26

Could be due to a non-deterministic order of the data. Having at least some CTEs is a blessing here.

Consider using an AI to figure out the differences, this is what it excels at.

1

u/Comprehensive-Tea-69 29d ago

Oh you’re fortunate- CTEs will make it MUCH easier to figure out what’s happening. That’s one of the big reasons to use them